前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ggplot2优雅的绘制配对连线云雨图

ggplot2优雅的绘制配对连线云雨图

作者头像
R语言数据分析指南
发布2022-12-20 20:06:35
7120
发布2022-12-20 20:06:35
举报

❝本节来介绍如何使用ggplot2绘制配对连线云雨图,图形倒也简单主要是细节;小编给了两个案例来进行展示,有循环绘图需求的可以看最后一个案例;❞

加载R包

代码语言:javascript
复制
library(tidyverse)
library(ggsignif)
library(gghalves)
library(ggsci)

数据清洗

代码语言:javascript
复制
df <- read_tsv("data.xls") %>%  
  filter(year %in% c(1957,2007),continent !="Oceania") %>% 
  select(country,year,lifeExp,continent)%>%
  mutate(paired = rep(1:(n()/2),each=2),year=factor(year))

数据可视化

代码语言:javascript
复制
df %>%
  ggplot(aes(year,lifeExp))+
  geom_half_violin(aes(split=year),side=2,alpha = 0.8)+
  stat_boxplot(geom="errorbar",width=0.1)+
  geom_boxplot(width=0.2)+
  geom_line(aes(group=paired),color="grey80") +
  geom_point(aes(fill=year,group=paired,size=lifeExp,alpha=lifeExp),pch=21,
             position = position_dodge(0.2))+
  scale_size_continuous(range=c(1,3))+
  geom_signif(comparisons = list(c("1957","2007")),
              map_signif_level=T,vjust=0.5,color="black",
              textsize=5,test=wilcox.test,step_increase=0.1)+
  facet_wrap(.~continent,nrow=1)+
  scale_fill_npg()+
  scale_y_continuous(limits = c(0,90),minor_breaks = seq(0,90,5))+
  labs(x=NULL,y=NULL)+
  theme_test()+
  theme(plot.margin=unit(c(0.5,0.5,0.5,0.5),units=,"cm"),
        axis.line = element_line(color = "black",size = 0.4),
        panel.grid.minor = element_blank(),
        panel.grid.major = element_line(size = 0.2,color = "#e5e5e5"),
        axis.text.y = element_text(color="black",size=10),
        axis.text.x = element_text(margin = margin(t = 2),color="black",size=10),
        legend.position = "none",
        panel.spacing = unit(0,"lines"))+
  coord_cartesian()

❝上面的案例我们使用了分面的形式来绘制图,但是实际中大家也许需要使用循环进行批量绘图,下面介绍循环绘图的具体代码 ❞

循环绘图

代码语言:javascript
复制
continents <- unique(df$continent)

plots <- map(continents, function(continent) {
  df %>% filter(continent == continent) %>%
    ggplot(aes(year,lifeExp))+
    geom_half_violin(aes(split=year),side=2,alpha = 0.8)+
    stat_boxplot(geom="errorbar",width=0.1)+
    geom_boxplot(width=0.2)+
    geom_line(aes(group=paired),color="grey80") +
    geom_point(aes(fill=year,group=paired,size=lifeExp,alpha=lifeExp),pch=21,
               position = position_dodge(0.2))+
    scale_size_continuous(range=c(1,3))+
    geom_signif(comparisons = list(c("1957","2007")),
                map_signif_level=T,vjust=0.5,color="black",
                textsize=5,test=wilcox.test,step_increase=0.1)+
    scale_fill_npg()+
    scale_y_continuous(limits = c(0,90),minor_breaks = seq(0,90,5))+
    labs(x=NULL,y=NULL)+theme_test()+
    theme(plot.margin=unit(c(0.5,0.5,0.5,0.5),units=,"cm"),
          axis.line = element_line(color = "black",size = 0.4),
          panel.grid.minor = element_blank(),
          panel.grid.major = element_line(size = 0.2,color = "#e5e5e5"),
          axis.text.y = element_text(color="black",size=10),
          axis.text.x = element_text(margin = margin(t = 2),color="black",size=10),
          legend.position = "none",panel.spacing = unit(0,"lines"))+
    coord_cartesian()
})

保存图片

代码语言:javascript
复制
map2(plots, continents, function(plot, continent) {
  ggsave(plot, filename = paste0("plot_", continent, ".pdf"))
})
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-12-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 R语言数据分析指南 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 加载R包
  • 数据清洗
  • 数据可视化
  • 循环绘图
  • 保存图片
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档