查看原文
其他

ggplot2分面图形大改造

阿越就是我 医学和生信笔记 2023-06-15
关注公众号,发送R语言Python,获取学习资料!


ggplot2的默认分面功能功能不够强大,支持的自定义参数也比较少,今天介绍的这个包可以对分面进行超多改头换面的操作!

  • 安装

  • 使用

    • facet_wrap2()/facet_grid2()

    • facet_nest()

    • facet_manual()

    • 分面的条带设置

安装

install.packages("ggh4x")

# install.packages("devtools")
devtools::install_github("teunbrand/ggh4x")

使用

首先我们建立一个默认的分面图形。

library(ggplot2)

p <- ggplot(mpg, aes(displ, hwy, colour = as.factor(cyl))) + geom_point() +
  labs(x = "Engine displacement", y = "Highway miles per gallon") +
  guides(colour = "none")
p + facet_wrap(~ class)
plot of chunk unnamed-chunk-2

默认的分面是上面这样的,而今天要介绍的ggh4x提供了更加强大的函数gacet_wrap2()

facet_wrap2()/facet_grid2()

默认情况下和facet_wrap()函数完全相同:

library(ggh4x)

p + facet_wrap2(vars(class))
plot of chunk unnamed-chunk-3

支持为每一个分面图增加坐标刻度,并支持移除或保留x轴或者y轴的标签:

p + facet_wrap2(vars(class), axes = "all", remove_labels = "x")
plot of chunk unnamed-chunk-4

支持更加强大的分面图形布局,还可以进行留白:

p + facet_wrap2(vars(class), nrow = 4, ncol = 4, trim_blank = F)
plot of chunk unnamed-chunk-5

facet_wrap()是针对一个变量进行分面的,facet_grid()是针对两个变量进行分面的,所以ggh4x也提供了facet_grid2()函数扩展其功能!

p + facet_grid2(vars(year), vars(drv), axes = "all", remove_labels = "y")
plot of chunk unnamed-chunk-6

默认的facet_grid()函数中的scales参数,只能控制整体的x轴或者y轴,但是facet_grid2()函数提供了额外的independent参数,可以让所有分面的坐标都自行调节(不明白的可以用默认的facet_grid画图比较下)。

p + facet_grid2(vars(year), vars(drv), scales = "free_x", independent = "x")
plot of chunk unnamed-chunk-7

facet_nest()

嵌套分面,可以实现以下效果,非常实用。

new_iris <- transform(
  iris, 
  Nester = ifelse(Species == "setosa""Short Leaves""Long Leaves")
)

iris_plot <- ggplot(new_iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point()

iris_plot +
  facet_nested(~ Nester + Species) # 分面在一起,嵌套
plot of chunk unnamed-chunk-8

还可以对嵌套分面进行各种细节调整:

iris_plot +
  facet_nested(~ Nester + Species, nest_line = element_line(linetype = 2)) + # 线型
  theme(strip.background = element_blank(),
        ggh4x.facet.nestline = element_line(colour = "blue")) # 颜色
plot of chunk unnamed-chunk-9

可以把分面标签放在下面:

iris_plot +
  geom_point(data = ~ transform(.x, Species = NULL, Nester = "All")) + # 添加一个包含所有点的分面
  facet_nested(~ Nester + Species, switch = "x"# 放在下面
plot of chunk unnamed-chunk-10

还提供了facet_nested_wrap()函数,作为facet_wrap()的变体:

p + 
  facet_nested_wrap(
    vars(cyl, drv), dir = "v", strip.position = "left",
    axes = "all", remove_labels = "x"
  ) +
  theme(strip.placement = "outside")
plot of chunk unnamed-chunk-11

facet_manual()

为分面图形提供更加强大的布局,支持自定义各种复杂的自定义布局。

design <- matrix(c(1,2,3,2), 22)

p + facet_manual(vars(factor(drv)), design = design)
plot of chunk unnamed-chunk-12

也支持使用类似patchwork::plot_layout()风格的布局参数:使用连续的字母代替图形,不同的行数代表行,#代表占位符:

design <- "
  A##
  AB#
  #BC
  ##C
"

p + facet_manual(vars(drv), design = design)
plot of chunk unnamed-chunk-13

使用heights/widths参数控制布局中每行和每列的高度和宽度:

p + facet_manual(
  vars(drv), design = design,
  heights = 4:1# 4行,每行的高度依次是4,3,2,1
  widths = unit(1:3"cm"# 3列,每列的宽度依次是1,2,3
)
plot of chunk unnamed-chunk-14

只要给出合适的布局,重叠也是可以的:

design <- "
  AA#
  ACB
  #BB
"

p + facet_manual(vars(drv), design = design)
plot of chunk unnamed-chunk-15

分面的条带设置

提供了strip_vanilla()/strip_themed()/strip_nested()函数用以控制strip的诸多细节。

p2 <- p +
  theme(strip.background = element_rect(colour = "black", size = 2),
        axis.line.y = element_line(colour = "black", size = 2))
p2
plot of chunk unnamed-chunk-16
p2 + facet_wrap2(vars(year), strip = strip_vanilla(clip = "on")) +
  ggtitle('clip = "on"')
plot of chunk unnamed-chunk-16
p2 + facet_wrap2(vars(year), strip = strip_vanilla(clip = "off")) +
  ggtitle('clip = "off"')
plot of chunk unnamed-chunk-16
df <- data.frame(
  long = paste("A long title that is going to make the\n",
               "smaller title take up too much space"),
  short = LETTERS[1:3],
  x = 1:3, y = 1:3
)
p2 <- ggplot(df, aes(x, y)) +
  geom_point() +
  theme(strip.text.y.left = element_text(angle = 0),
        strip.placement = "outside",
        plot.title.position = "plot")
p2
plot of chunk unnamed-chunk-17
p2 + facet_grid2(long + short ~ ., switch = "y",
                strip = strip_vanilla(size = "constant")) +
  ggtitle('size = "constant"')
plot of chunk unnamed-chunk-17
p2 + facet_grid2(long + short ~ ., switch = "y",
                strip = strip_vanilla(size = "variable")) +
  ggtitle('size = "variable"')
plot of chunk unnamed-chunk-18
ridiculous_strips <- strip_themed(
     # Horizontal strips
     background_x = elem_list_rect(fill = c("limegreen""dodgerblue")),
     text_x = elem_list_text(colour = c("dodgerblue""limegreen"),
                             face = c("bold""bold")),
     by_layer_x = TRUE,
     # Vertical strips
     background_y = elem_list_rect(
       fill = c("gold""tomato""deepskyblue")
     ),
     text_y = elem_list_text(angle = c(090)),
     by_layer_y = FALSE
)

p + facet_grid2(class ~ drv + year, strip = ridiculous_strips)
plot of chunk unnamed-chunk-19
p + facet_grid2(
   . ~ drv + year,
   strip = strip_themed(
     background_x = list(NULL, element_rect(colour = "black"), element_blank(),
                         element_rect(fill = "black")),
     text_x = list(NULLNULLNULL, element_text(colour = "white"))
   )
)
plot of chunk unnamed-chunk-20
p + facet_wrap2(
  vars(cyl, drv), ncol = 4,
  strip = strip_nested(bleed = FALSE)
) +
  ggtitle('bleed = FALSE')
plot of chunk unnamed-chunk-21
p + facet_grid2(
  cols = vars("Outer label""Inner label"),
  switch = "x", strip = strip_vanilla()
) +
  ggtitle("strip_vanilla()")
plot of chunk unnamed-chunk-22
p + facet_grid2(
  cols = vars("Outer label""Inner label"),
  switch = "x", strip = strip_nested()
) +
  ggtitle("strip_nested()")
plot of chunk unnamed-chunk-22


以上就是今天的内容,希望对你有帮助哦!欢迎点赞、在看、关注、转发


欢迎扫描二维码加 QQ群 613637742


欢迎关注公众号:医学和生信笔记




往期回顾

画一个好看的森林


用更简单的方式画森林


使用R语言画森林图和误差线(合辑)


R语言和医学统计学(合辑)


R语言和医学统计学系列(2):方差分析


R语言和医学统计学系列(4):秩和检验


R语言和医学统计学系列(3):卡方检验

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存