ggplot2基础绘图之哑铃图

2022-09-21 14:38:55 浏览数 (1)

❝本节来分享一个绘制哑铃图的小教程,里面细节满满;各位观众老爷细细品味,下面来看具体案例 ❞

导入数据

代码语言:javascript复制
read_tsv("data.xls") %>% 
  distinct(country_name,.keep_all = T)

数据可视化

代码语言:javascript复制
ggplot(aes(reorder(country_name, duration)))  
  geom_point(aes(y = start_year, col = continent), size = 5, show.legend = FALSE)  
  geom_point(aes(y = end_year, col = continent), size = 5, show.legend = FALSE)  
  geom_segment(aes(xend = country_name, y = start_year, yend = end_year, col = continent), size = 4)  
  geom_segment(aes(xend = country_name, y = start_year, yend = end_year), col = "white", size = 2)  
  geom_point(aes(y = start_year), size = 3, col = "white")  
  geom_point(aes(y = end_year), size = 3, col = "white")  
  geom_text(aes(y = 2023,label = paste(duration,"year")),color="black",size=3)  
  scale_y_continuous() 
  coord_flip()  
  scale_color_aaas() 
  labs(x = NULL, y = NULL, col = NULL)  
  theme(axis.title = element_blank(),
        axis.text.x=element_text(color="black",angle = 0,vjust=0.5, margin = margin(b =5)),
        axis.text.y = element_markdown(size = 10,hjust =1,color = "black"),
        panel.background = element_rect(fill = NA,color = NA),
        panel.grid.minor= element_line(size=0.2,color="#e5e5e5"),
        panel.grid.major = element_line(size=0.2,color="#e5e5e5"),
        panel.border = element_rect(fill=NA,color="black",size=1,linetype="solid"),
        legend.position = "top",
        legend.direction="horizontal",#"vertical"
        legend.title = element_blank(),
        legend.background=element_blank())

0 人点赞