美文网首页R可视化
跟着Nature Metabolism学作图:R语言ggplot

跟着Nature Metabolism学作图:R语言ggplot

作者: 小明的数据分析笔记本 | 来源:发表于2023-02-20 21:56 被阅读0次

    论文

    Single-cell profiling of vascular endothelial cells reveals progressive organ-specific vulnerabilities during obesity

    https://www.nature.com/articles/s42255-022-00674-x#Sec58

    s42255-022-00674-x.pdf

    https://github.com/Osynchronika/sc_EC_obesity_atlas

    大部分 作图的数据都有,可以试着用论文中提供的数据复现一下论文中的图

    今天的推文我们复现一下论文中的figure4c 分组折线图并添加误差线,并且在指定区域添加一个灰色背景

    image.png

    论文中提供的示例数据如下

    image.png

    手动整理成如下格式

    image.png

    加载作图需要用到的R包

    library(readxl)
    library(tidyverse)
    library(plotrix)
    library(ggh4x)
    

    读取数据

    dat<-read_excel("data/20230207/figure4c.xlsx")
    dat
    

    按照行计算平均值和标准误

    dat %>% 
      rowwise() %>% 
      mutate(mean_value=mean(c_across(rep1:rep4)),
             std_value=std.error(c_across(rep1:rep4))) %>% 
      filter(Weeks>=11)-> new.dat
    

    作图代码

    ggplot(data=new.dat,aes(x=Weeks,y=mean_value))+
      annotate(geom = "rect",xmin=22,xmax=27,ymin=-Inf,ymax=Inf,fill="gray")+
      geom_vline(xintercept = 22,lty="dashed",size=1,color="black")+
      geom_point(aes(color=group),size=3)+
      geom_line(aes(color=group),size=1)+
      scale_x_continuous(limits = c(10,27),
                         breaks = seq(10,25,5),
                         guide = "axis_minor",
                         minor_breaks = seq(11,27,1),
                         expand = expansion(mult=c(0,0.05)))+
      scale_y_continuous(limits = c(20,50),
                         breaks = seq(20,50,by=10),
                         expand=expansion(mult=c(0,0)))+
      guides(x=guide_axis_minor(trunc_upper = 27,
                                trunc_lower = 10))+
      geom_errorbar(aes(ymin=mean_value-std_value,
                        ymax=mean_value+std_value,
                        color=group),
                    width=0.2,
                    size=1)+
      scale_color_manual(values = c("WD"="#f68a15",
                                    "Rev 3"="#008c00",
                                    "Chow"="#094cc3"))+
      theme_classic()+
      theme(ggh4x.axis.ticks.length.minor= rel(0.5),
            axis.ticks.length.x = unit(0.5,'lines'),
            legend.position = "bottom")+
      labs(x="Age(weeks)",y=expression(Weight%+-%s.e.m.(g)))+
      annotate(geom = "text",x=27,y=41,label="P=0.003",hjust=1)+
      annotate(geom = "text",x=27,y=35,label="P=0.007",hjust=1)+
      coord_cartesian(clip="off")
    
    image.png

    做的最后才发现是用figure4d的数据做的图,我说怎么看起来和figure4c差的有点多

    示例数据和代码可以给推文点赞,然后点击在看,最后留言获取

    欢迎大家关注我的公众号

    小明的数据分析笔记本

    小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!

    微信公众号好像又有改动,如果没有将这个公众号设为星标的话,会经常错过公众号的推文,个人建议将 小明的数据分析笔记本 公众号添加星标,添加方法是

    点开公众号的页面,右上角有三个点

    image.png

    点击三个点,会跳出界面

    image.png

    直接点击 设为星标 就可以了

    相关文章

      网友评论

        本文标题:跟着Nature Metabolism学作图:R语言ggplot

        本文链接:https://www.haomeiwen.com/subject/hcogkdtx.html