前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >跟着Global Change Biology学作图:R语言ggplot2点线图(1)

跟着Global Change Biology学作图:R语言ggplot2点线图(1)

作者头像
用户7010445
发布2023-01-06 20:27:05
4480
发布2023-01-06 20:27:05
举报

论文

Changes in plant inputs alter soil carbon and microbial communities in forest

本地pdf Global Change Biology - 2022 - Feng - Changes in plant inputs alter soil carbon and microbial communities in forest.pdf

今天的推文重复一下论文中的Figure3,这个是之前有读者在公众号后台的留言,之前我不知道怎么实现这种好几个子图中间没有空白的形式,有读者留言可以用分面然后调节主题里的参数panel.spacing = unit(0,'lines'),有了基本思路就可以尝试一下这个图

image.png

今天推文的主要内容是实现这个图的主题部分,四周的一些注释,分面图在指定的子图添加文本等等 再单独的一篇推文来介绍

添加注释会用到一个自定义函数

代码语言:javascript
复制
annotation_custom2 <- function (grob, 
                                xmin = -Inf, 
                                xmax = Inf, 
                                ymin = -Inf, 
                                ymax = Inf, 
                                data) 
{
  layer(data = data, 
        stat = StatIdentity, 
        position = PositionIdentity, 
        geom = ggplot2:::GeomCustomAnn,
        inherit.aes = TRUE, params = list(grob = grob, 
                                          xmin = xmin, xmax = xmax, 
                                          ymin = ymin, ymax = ymax))
}

这个是在网上找到的,具体来源想不起来了

首先是示例数据集截图

image.png

这个不是论文中提供的,是我自己随便构造的

读取数据集

代码语言:javascript
复制
library(readxl)

dat<-read_excel("data/20220804/example_df.xlsx")
dat

最基本的点和误差线

代码语言:javascript
复制
library(ggplot2)

ggplot(data=dat,aes(x=x,y=y))+
  geom_errorbarh(aes(xmin=x-sd_value,
                     xmax=x+sd_value,
                     color=group02),
                 height=0,
                 show.legend = FALSE)+
  geom_point(aes(color=group02),
             size=5,
             show.legend = TRUE)

image.png

分面加一些主题设置

代码语言:javascript
复制
ggplot(data=dat,aes(x=x,y=y))+
  geom_errorbarh(aes(xmin=x-sd_value,
                     xmax=x+sd_value,
                     color=group02),
                 height=0,
                 show.legend = FALSE)+
  geom_point(aes(color=group02),
             size=5,
             show.legend = TRUE)+
  facet_wrap(~group01)+
  theme_bw()+
  theme(panel.spacing = unit(0,'lines'),
        panel.background = element_blank(),
        axis.line = element_line(),
        panel.grid = element_blank(),
        axis.ticks.y = element_blank(),
        strip.text = element_text(hjust = 0))

image.png

添加一些虚线,坐标轴设置

代码语言:javascript
复制
ggplot(data=dat,aes(x=x,y=y))+
  geom_errorbarh(aes(xmin=x-sd_value,
                     xmax=x+sd_value,
                     color=group02),
                 height=0,
                 show.legend = FALSE)+
  geom_point(aes(color=group02),
             size=5,
             show.legend = TRUE)+
  facet_wrap(~group01)+
  theme_bw()+
  theme(panel.spacing = unit(0,'lines'),
        panel.background = element_blank(),
        axis.line = element_line(),
        panel.grid = element_blank(),
        axis.ticks.y = element_blank(),
        strip.text = element_text(hjust = 0))+
  labs(y="",x=NULL)+
  scale_x_continuous(limits = c(-30,30),
                     breaks = seq(-30,30,10))+
  scale_y_continuous(limits = c(0.5,3.5),
                     breaks = c(1,2,3),
                     labels=c("SOC concentration",
                              "SOC stock",
                              "SOC concentration"),
                     expand = expansion(mult = c(0,0)))+
  geom_vline(xintercept = 0,
             color="gray",
             lty="dashed")+
  geom_hline(yintercept = 1,
             color="gray",
             lty="dashed")+
  geom_hline(yintercept = 2,
             color="gray",
             lty="dashed")+
  geom_hline(yintercept = 3,
             color="gray",
             lty="dashed") -> p0
p0

最后是自定义颜色

代码语言:javascript
复制
p0+
  scale_color_manual(values = c(
    "A"="#b856d7",
    "B"="#55a0fb",
    "C"="#0f99b2",
    "D"="#ffa040",
    "E"="#008000"
  ),
  labels=c(
    "A"="Litter addition",
    "B"="Litter removal",
    "C"="Root removal",
    "D"="Litter effect",
    "E"="Root effect"
    ))+
  theme(legend.position = "bottom",
        legend.title = element_blank())
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-09-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小明的数据分析笔记本 微信公众号,前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 论文
  • 首先是示例数据集截图
  • 读取数据集
  • 最基本的点和误差线
  • 分面加一些主题设置
  • 添加一些虚线,坐标轴设置
  • 最后是自定义颜色
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档