加载R包
代码语言:javascript
复制library(tidyverse)
导入数据
代码语言:javascript
复制df <- read_tsv("data.xls")
定义因子
代码语言:javascript
复制df$month_label <- factor(df$month_label,levels = df$month_label)
数据可视化
代码语言:javascript
复制merged |>
ggplot()
aes(x = month_label)
geom_line(aes(y = p_expected, group = 1),
linetype = "dashed")
geom_linerange(aes(ymin = p_births, ymax = p_nhl))
geom_point(aes(y = p_births, color = "Canadian Population"), size = 3)
geom_point(aes(y = p_nhl, color = "Canadian NHL Players"), size = 3)
geom_text(aes(label = scales::percent(p_births, accuracy = 0.01),
y = if_else(p_births > p_nhl, p_births .007,
p_births - .007)), size = 3)
geom_text(aes(label = scales::percent(p_nhl, accuracy = 0.007),
y = if_else(p_nhl > p_births, p_nhl .01,
p_nhl - .007)), size = 3)
coord_flip()
scale_x_discrete(limits = rev(levels(merged$month_label)))
labs(x = "Birth Month",y = "Percent")
scale_color_manual(values = my_pal)
theme_bw()
theme(legend.position = "bottom",
legend.title = element_blank(),
axis.text.y=element_text(color="black",size=10,face="bold"),
axis.text.x=element_text(color="black",size=10,face="bold"),
axis.title.x = element_text(margin = margin(t = 0.5,unit="cm"),
color="black",face="bold"),
axis.title.y = element_text(margin = margin(r = 0.5,unit="cm"),
color="black",face="bold"),
plot.margin = margin(0.5,1,0.5,0.5,unit="cm"))