R语言包ggplot实现分面去掉小标题的灰色底色小方法

目录
当我们在使用 ggplot 时,使用分面通常会长下面这样(这里用 ggplot 的官方案例):
 
p <- ggplot(mpg, aes(displ, hwy)) + geom_point()
p + facet_wrap(~class)
 
 
此时,我们想将背景的灰色底色去掉,可以用我们常用的 theme_bw():
 
p + facet_wrap(~class) + theme_bw()
 
 
此时如果背景的灰色网格不想要了,可以接着添加 theme(panel.grid = element_blank()):
 
p + facet_wrap(~class) + theme_bw() +
  theme(panel.grid = element_blank())
 
 
但此时我们是想纯白的背景,子标题中的灰色背景与标题边框也不需要了,此时可以在 theme() 函数中添加 element_rect(),内部的参数 color = "white" 表示 子标题的周围边框变为白色;fill = "white" 表示子标题的灰色背景变为白色:
 
p + facet_wrap(~class) + theme_bw() +
  theme(
    strip.background = element_rect(
      color = "white", fill = "white"),
    panel.grid = element_blank())
 
 
这样看起来还是挺丑的,如果把每个画幅的一圈黑色边框去掉就更好了,此时可以添加 panel.border = element_blank():
 
p + facet_wrap(~class) + theme_bw() +
  theme(
    strip.background = element_rect(
      color = "white", fill = "white"),
    panel.grid = element_blank(),
    panel.border = element_blank())
 
 
后面如果大家还想让整个图像变得更加清净,可以参考:R语言学习ggplot2绘制统计图形包全面详解
 
里面的 6. 零件打磨 部分。

dawei

【声明】:绥化站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。