美文网首页
Learn R package together--(3)

Learn R package together--(3)

作者: 凯凯何_Boy | 来源:发表于2020-08-09 15:23 被阅读0次

    论文中作图经常会有合并图排版需求,一般会放入PPT中或AI手动调节,若是在R中出图前直接调好岂不是更美滋滋,So本次就介绍几种关于合并图形的包cowplot、gridExtra、ggpubr、patchwork

    图形合并策略

    前三种策略

    library(cowplot)
    library(gridExtra)
    library(ggpubr)
    data("mtcars")
    data("ToothGrowth")
    rm(list = ls())
    # 前三种策略
    mtcars$name <- rownames(mtcars)
    mtcars$cyl <- as.factor(mtcars$cyl)
    mtcars
    mtcars$name <- factor(mtcars$name,levels = mtcars[order(mtcars$cyl),'name'])#此处设置可使同组排列一起,调整因子的顺序
    ToothGrowth$dose <- as.factor(ToothGrowth$dose)
    bxp <- ggplot(ToothGrowth,aes(x=dose,y=len,color=dose))+geom_boxplot()
    
    dp <- ggplot(ToothGrowth,aes(x=dose,y=len,fill=dose))+geom_dotplot(binaxis='y',stackdir = 'center',
                                                                   stackratio = 1.5,dotsize = 1.2)
    
    bp <- ggplot(mtcars,aes(x=name,y=mpg,fill=cyl))+geom_bar(stat = 'identity')+theme(axis.text.x=element_text(angle = 75,hjust=1,size=8)) # 
    
    sp <- ggplot(mtcars,aes(x=wt,y=mpg,color=cyl,shapecyl))+geom_point()+geom_smooth(method = ls,aes(fill=cyl))
    
    plot_grid(bxp,dp,bp,sp,labels = c('A','B','C','D'),ncol = 2,nrow = 2) #cowplot
    ggarrange(bxp,dp,bp,sp,labels = c('A','B','C','D'),ncol = 2,nrow = 2) #ggpubr
    grid.arrange(bxp,dp,bp,sp,ncol = 2,nrow = 2) #gridExtra
    
    图片.png

    第四种策略用新包patchwork

    # install.packages('patchwork')
    library('patchwork')
    bxp+dp+bp+sp + plot_annotation(tag_levels = 'A')+ #图例都放右边
      plot_layout(guides = 'collect')& theme_minimal() #&符号可以修改所有子图主题
    
    
    图片.png

    个人比较看好patchwork,对于排版有个更大的自由,参数有很多,想了解的话直接看大佬的原文档

    相关文章

      网友评论

          本文标题:Learn R package together--(3)

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