### Packages
library(ggplot2)
library(dplyr)
library(forcats)
library(ggridges) # data: lincoln_weather, geom_density_ridges()
– 가로축에 기준을 둔 분포 상태의 시각화
### Figure 9.3 - data
data(lincoln_weather)
lincoln_weather %>% sapply(class)
CST Max Temperature [F] Mean Temperature [F] Min Temperature [F]
"character" "integer" "integer" "integer"
Max Dew Point [F] Mean Dew Point [F] Min Dewpoint [F] Max Humidity
"integer" "integer" "integer" "integer"
Mean Humidity Min Humidity Max Sea Level Pressure [In] Mean Sea Level Pressure [In]
"integer" "integer" "numeric" "numeric"
Min Sea Level Pressure [In] Max Visibility [Miles] Mean Visibility [Miles] Min Visibility [Miles]
"numeric" "integer" "integer" "integer"
Max Wind Speed [MPH] Mean Wind Speed[MPH] Max Gust Speed [MPH] Precipitation [In]
"integer" "integer" "integer" "character"
CloudCover Events WindDir [Degrees] Month
"integer" "character" "integer" "factor"
lincoln_df <- lincoln_weather %>%
mutate(
month_short = fct_recode(
Month,
Jan = "January",
Feb = "February",
Mar = "March",
Apr = "April",
May = "May",
Jun = "June",
Jul = "July",
Aug = "August",
Sep = "September",
Oct = "October",
Nov = "November",
Dec = "December"
)
) %>%
mutate(month_short = fct_rev(month_short))
### Figure 9.3
ggplot(lincoln_df, aes(x = month_short, y = `Mean Temperature [F]`)) +
geom_boxplot(fill = 'grey90') +
xlab("month") +
ylab("mean temperature (°F)") +
theme_classic() +
theme(plot.margin = margin(3, 7, 3, 1.5))
### Figure 9.5
ggplot(lincoln_df, aes(x = month_short, y = `Mean Temperature [F]`)) +
geom_violin(fill = 'grey90') +
xlab("month") +
ylab("mean temperature (°F)") +
theme_classic() +
theme(plot.margin = margin(3, 7, 3, 1.5))
### Figure 9.7
ggplot(lincoln_df, aes(x = month_short, y = `Mean Temperature [F]`)) +
geom_point(position = position_jitter(width = .15, height = 0, seed = 320), size = 1) +
xlab("month") +
ylab("mean temperature (°F)") +
theme_classic() +
theme(plot.margin = margin(3, 7, 3, 1.5))
### Figure 9.8
ggplot(lincoln_df, aes(x = month_short, y = `Mean Temperature [F]`)) +
geom_violin(color = "transparent", fill = "gray90") +
geom_point(position = position_jitter(width = .15, height = 0, seed = 320), size = 1) +
xlab("month") +
ylab("mean temperature (°F)") +
theme_classic() +
theme(plot.margin = margin(3, 7, 3, 1.5))
### Figure 9.9
ggplot(lincoln_df, aes(x = `Mean Temperature [F]`, y = `Month`)) +
geom_density_ridges(
scale = 3, rel_min_height = 0.01,
bandwidth = 3.4, fill = "skyblue", color = "white"
) +
scale_x_continuous(
name = "mean temperature (°F)",
expand = c(0, 0), breaks = c(0, 25, 50, 75)
) +
scale_y_discrete(name = NULL) +
theme_minimal() +
theme(
axis.text.y = element_text(vjust = 0),
plot.margin = margin(3, 7, 3, 1.5)
)
### Figure 9.11 - data
movie_lengths <- read.csv("./data/movie_lengths.csv")
### Figure 9.11
ggplot(movie_lengths, aes(x = length, y = year, group = year)) +
geom_density_ridges(scale = 10, size = 0.25, rel_min_height = 0.03,
fill = "grey85", na.rm = TRUE) +
scale_x_continuous(limits = c(0, 200), expand = c(0, 0), name = "length (minutes)") +
scale_y_reverse(
breaks = c(2000, 1980, 1960, 1940, 1920),
limits = c(2005, 1903), expand = c(0, 0)
) +
theme_light() +
theme(
panel.border = element_blank(),
plot.margin = margin(3, 14, 3, 1.5)
)
load("./data/dw_nominate_house.rda")
### U.S. House 1963-2013
all_house_88_113 <- dw_nominate_house %>%
filter(congress >= 88 & cd !=0 & cd != 98 & cd != 99) %>%
filter(party_code == 100 | party_code == 200) %>%
arrange(desc(congress)) %>% mutate(year1 = congress * 2 + 1787) %>%
arrange(desc(year1))
all_house_88_113 %>% write.csv("all_house_88_113.csv", row.names = F)
### Figure 9.12 - data
all_house_88_113 <- read.csv("all_house_88_113.csv")
### Figure 9.12
ggplot(all_house_88_113,
aes(
x = dim_1,
y = year1,
group = interaction(party_code, factor(year1)),
fill = interaction(party_code, factor(year1))
)
) +
geom_density_ridges(scale = 5, size = 0.25, rel_min_height = 0.01,
alpha=0.9, color = "white") +
scale_x_continuous(
name = "DW-NOMINATE score",
limits = c(-.8, 1.3),
breaks = seq(-1, 1, 0.25)
) +
scale_y_reverse(
name = "year",
expand = c(0, 0), breaks=c(seq(2013, 1963, -10))
) +
scale_fill_cyclical(
breaks = c("100.1963", "200.1963"),
labels = c(`100.1963` = "Democrats ", `200.1963` = "Republicans"),
values = c("#4040ff", "#ff4040", "#6060ff", "#ff6060"),
name = NULL,
guide = "legend"
) +
theme_light() +
theme(
panel.border = element_blank(),
axis.text.y = element_text(vjust = 0),
legend.position.inside = c(1, 1),
legend.justification = c(1, 1),
legend.direction = "horizontal",
legend.background = element_rect(fill = "white")
)
## 기상청 자료
air <- read.csv("./data/OBS_ASOS_DD_20230322080932.csv", fileEncoding = "CP949")
air %>% head()
sapply(air, class)
지점 지점명 일시 평균기온..C. 최저기온..C. 최고기온..C.
"integer" "character" "character" "numeric" "numeric" "numeric"
### 자료형 변경 후 월 변수 생성성
air$일시 <- lubridate::ymd(air$일시)
air$월 <- lubridate::month(air$일시, label = T)
air %>% head()
### Figure 9.3
air %>% filter(지점명=="대전") %>% ggplot( aes(x = 월, y = 평균기온..C.)) +
geom_boxplot(fill = 'grey90') +
xlab("월") +
ylab("평균 기온 (°C)") +
theme_classic() +
theme(plot.margin = margin(3, 7, 3, 1.5))
### Figure 9.5
air %>% filter(지점명=="대전") %>% ggplot( aes(x = 월, y = 평균기온..C.)) +
geom_violin(fill = 'grey90') +
xlab("월") +
ylab("평균 기온 (°C)") +
theme_classic() +
theme(plot.margin = margin(3, 7, 3, 1.5))
### Figure 9.7
air %>% filter(지점명=="대전") %>% ggplot( aes(x = 월, y = 평균기온..C.)) +
geom_point(position = position_jitter(width = .15, height = 0, seed = 320), size = 0.75) +
xlab("월") +
ylab("평균 기온 (°C)") +
theme_classic() +
theme(plot.margin = margin(3, 7, 3, 1.5))
### Figure 9.8
air %>% filter(지점명=="대전") %>% ggplot( aes(x = 월, y = 평균기온..C.)) +
geom_violin(color = "transparent", fill = "gray90") +
geom_point(position = position_jitter(width = .15, height = 0, seed = 320), size = 0.75) +
xlab("월") +
ylab("평균 기온 (°C)") +
theme_classic() +
theme(plot.margin = margin(3, 7, 3, 1.5))
### Figure 9.9
air %>% filter(지점명=="대전") %>% ggplot(aes(x = 평균기온..C., y = fct_rev(월))) +
geom_density_ridges(
scale = 3, rel_min_height = 0.01,
bandwidth = 2, fill = "skyblue", color = "white"
) +
scale_x_continuous(
name = "평균 기온 (°C)",
expand = c(0, 0)
) +
scale_y_discrete(name = "월") +
theme_minimal() +
theme(
axis.text.y = element_text(vjust = 0),
plot.margin = margin(3, 7, 3, 1.5)
)
### Figure 9.12
#with(air, interaction(지점명, factor(월)))
air %>% filter(지점명 %in% c("대전", "제주")) %>% ggplot(
aes(
x = 평균기온..C.,
y = fct_rev(월),
group = interaction(지점명, factor(월)),
fill = interaction(지점명, factor(월))
)
) +
geom_density_ridges(rel_min_height = 0.01, alpha=0.9, color = "white") +
scale_x_continuous(name = "평균 기온 (°C)") +
scale_y_discrete(name = "월") +
scale_fill_cyclical(
breaks = c("대전.1", "제주.1"),
labels = c(`대전.1` = "대전", `제주.1` = "제주"),
values = c("#4040ff", "#ff4040", "#6060ff", "#ff6060"),
name = NULL,
guide = "legend"
) +
theme_light() +
theme(
panel.border = element_blank(),
axis.text.y = element_text(vjust = 0),
legend.position = c(1, 1),
legend.justification = c(1, 1),
legend.direction = "horizontal",
legend.background = element_rect(fill = "white")
)