美文网首页ggplot2绘图R作图
ggprism包数据可视化之轴外观设置

ggprism包数据可视化之轴外观设置

作者: R语言数据分析指南 | 来源:发表于2021-02-20 00:32 被阅读0次

    ggprism软件包提供了各种主题自定义ggplot2并赋予它们GraphPad Prism外观,今天介绍如何更改轴外观,喜欢的小伙伴可以关注个人公众号R语言数据分析指南持续分享更多优质资源,在此先行拜谢了!!

    library(ggplot2)
    library(ggprism)
    library(patchwork)
    

    向图中添加较小的刻度线有两种形式,使用连续刻度,例如scale_x_continuous(),或使用guides()函数。请注意guide_prism_minor()不适用于离散轴,因为它们没有细微的间断

    p <- ggplot(ToothGrowth, aes(x = factor(supp), y = len)) + 
      geom_boxplot(aes(fill = factor(supp))) + 
      theme_prism() + 
      theme(legend.position = "none")
    
    p1 <- p + scale_y_continuous(guide = guide_prism_minor())
    p2 <- p + guides(y = guide_prism_minor())
    
    p1 + p2
    

    要调整次刻度的数量,只需使用minor_breaks连续刻度函数的参数更改次中断的数量即可。给定minor_breaks参数将定义每个小刻度的位置

    p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
      stat_summary(aes(fill = factor(dose)), na.rm = TRUE,
                   geom = "col", fun = mean, colour = "black", size = 0.9) + 
      theme_prism() + 
      theme(legend.position = "none")
    
    p1 <- p + scale_y_continuous(guide = "prism_minor",
                                 limits = c(0, 30),
                                 expand = c(0, 0))
    p2 <- p + scale_y_continuous(guide = "prism_minor", 
                                 limits = c(0, 30),
                                 expand = c(0, 0),
                                 minor_breaks = seq(0, 30, 2))
    p1 + p2
    

    要获得log10次刻度,只需使用log10刻度,然后minor_breaks如上所述修改n参数即可

    p <- ggplot(msleep, aes(bodywt, brainwt)) +
      geom_point(na.rm = TRUE) + 
      theme_prism()
    
    p1 <- p + scale_x_log10(limits = c(1e0, 1e4),
                            guide = "prism_minor")
    p2 <- p + scale_x_log10(limits = c(1e0, 1e4),
                            minor_breaks = rep(1:9, 4)*(10^rep(0:3, each = 9)),
                            guide = "prism_minor")
    p1 + p2
    

    通过将小刻度线的长度设置为负数来更改其方向

    p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
      stat_summary(aes(fill = factor(dose)), na.rm = TRUE,
                   geom = "col", fun = mean, colour = "black", size = 0.9) + 
      theme_prism() + 
      scale_y_continuous(guide = "prism_minor", 
                                 limits = c(0, 30),
                                 expand = c(0, 0),
                                 minor_breaks = seq(0, 30, 2))
    
    p1 <- p + theme(legend.position = "none",
                    prism.ticks.length.y = unit(20, "pt"))
    p2 <- p + theme(legend.position = "none",
                    prism.ticks.length.y = unit(-20, "pt"))
    
    p1 + p2
    

    使用函数的axis.ticks参数更改主要刻度线的颜色时,次要刻度线的颜色(和其他美学属性)也将更改theme(

    p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
      stat_summary(aes(fill = factor(dose)), na.rm = TRUE,
                   geom = "col", fun = mean, colour = "black", size = 0.9) + 
      theme_prism() + 
      scale_y_continuous(guide = "prism_minor", 
                                 limits = c(0, 30),
                                 expand = c(0, 0),
                                 minor_breaks = seq(0, 30, 2))
    
    p1 <- p + theme(legend.position = "none")
    p2 <- p + theme(legend.position = "none",
                    axis.ticks.y = element_line(colour = "blue", 
                                              size = 2, 
                                              lineend = "round"))
    
    p1 + p2
    

    guide_prism_offset_minor()设置轴进行偏移设置
    p <- ggplot(ToothGrowth, aes(x = factor(supp), y = len)) + 
      geom_boxplot(aes(fill = factor(supp))) + 
      theme_prism() + 
      theme(legend.position = "none")
    
    p1 <- p + scale_y_continuous(guide = "prism_offset")
    p2 <- p + scale_y_continuous(limits = c(0, 40), guide = "prism_offset")
    
    p1 + p2
    
    p <- ggplot(ToothGrowth, aes(x = factor(supp), y = len)) + 
      geom_boxplot(aes(fill = factor(supp))) + 
      theme_prism() + 
      theme(legend.position = "none")
    
    p1 <- p + scale_y_continuous(guide = "prism_offset")
    p2 <- p + scale_y_continuous(guide = "prism_offset_minor")
    
    p1 + p2
    

    guide_prism_offset(),设置轴范围
    p <- ggplot(ToothGrowth, aes(x = factor(supp), y = len)) + 
      geom_boxplot(aes(fill = factor(supp))) + 
      theme_prism() + 
      theme(legend.position = "none")
    
    p1 <- p + scale_y_continuous(guide = "prism_offset_minor")
    p2 <- p + scale_y_continuous(limits = c(0, 40), 
                                 guide = "prism_offset_minor")
    
    p1 + p2
    

    guide_prism_minor()通过调整来更改次刻度的数量minor_breaks
    p <- ggplot(ToothGrowth, aes(x = factor(supp), y = len)) + 
      geom_boxplot(aes(fill = factor(supp))) + 
      theme_prism() + 
      theme(legend.position = "none")
    
    p1 <- p + scale_y_continuous(limits = c(0, 40), 
                                 guide = "prism_offset_minor")
    p2 <- p + scale_y_continuous(limits = c(0, 40), 
                                 minor_breaks = seq(0, 40, 2),
                                 guide = "prism_offset_minor")
    
    p1 + p2
    

    改变轴外观使之更适合离散数据

    p1 <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
      geom_jitter(aes(shape = factor(dose)), width = 0.2, size = 2) + 
      scale_shape_prism() + 
      theme_prism() + 
      theme(legend.position = "none") + 
      scale_y_continuous(limits = c(0, 40), guide = "prism_offset")
    
    p2 <- p1 + scale_x_discrete(guide = "prism_bracket")
    
    p1 + p2
    

    图像反转

    p1 <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
      geom_jitter(aes(shape = factor(dose)), width = 0.2, size = 2) + 
      scale_shape_prism() + 
      theme_prism() + 
      theme(legend.position = "none") + 
      scale_y_continuous(limits = c(0, 40), guide = "prism_offset") + 
      scale_x_discrete(guide = "prism_bracket")
    
    p2 <- p1 + coord_flip()
    
    p1 + p2
    

    使用outside参数更改括号的方向,默认情况下outside = TRUE这意味着括号指向外部

    p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
      geom_jitter(aes(shape = factor(dose)), width = 0.2, size = 2) + 
      scale_shape_prism() + 
      theme_prism() + 
      theme(legend.position = "none") + 
      scale_y_continuous(limits = c(0, 40), guide = "prism_offset")
    
    p1 <- p + scale_x_discrete(guide = "prism_bracket")
    p2 <- p + scale_x_discrete(guide = guide_prism_bracket(outside = FALSE))
    
    p1 + p2
    

    喜欢的小伙伴可以关注我的公众号

    R 语言数据分析指南
    主要分享数据可视化的一些经典案例及生物信息学习的资料及许多有趣的东西

    原文链接:https://mp.weixin.qq.com/s/YB7CdpbHfy2pEe42Yv6sLw

    相关文章

      网友评论

        本文标题:ggprism包数据可视化之轴外观设置

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