ggplot2分面图形大改造

2022-11-15 10:14:56 浏览数 (1)

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

  • 安装
  • 使用
    • facet_wrap2()/facet_grid2()
    • facet_nest()
    • facet_manual()
    • 分面的条带设置

安装

代码语言:javascript复制
install.packages("ggh4x")

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

使用

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

代码语言:javascript复制
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()函数完全相同:

代码语言:javascript复制
library(ggh4x)

p   facet_wrap2(vars(class))

plot of chunk unnamed-chunk-3

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

代码语言:javascript复制
p   facet_wrap2(vars(class), axes = "all", remove_labels = "x")

plot of chunk unnamed-chunk-4

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

代码语言:javascript复制
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()函数扩展其功能!

代码语言:javascript复制
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画图比较下)。

代码语言:javascript复制
p   facet_grid2(vars(year), vars(drv), scales = "free_x", independent = "x")

plot of chunk unnamed-chunk-7

facet_nest()

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

代码语言:javascript复制
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

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

代码语言:javascript复制
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

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

代码语言:javascript复制
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()的变体:

代码语言:javascript复制
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()

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

代码语言:javascript复制
design <- matrix(c(1,2,3,2), 2, 2)

p   facet_manual(vars(factor(drv)), design = design)

plot of chunk unnamed-chunk-12

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

代码语言:javascript复制
design <- "
  A##
  AB#
  #BC
  ##C
"
p   facet_manual(vars(drv), design = design)

plot of chunk unnamed-chunk-13

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

代码语言:javascript复制
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

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

代码语言:javascript复制
design <- "
  AA#
  ACB
  #BB
"
p   facet_manual(vars(drv), design = design)

plot of chunk unnamed-chunk-15

分面的条带设置

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

代码语言:javascript复制
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

代码语言:javascript复制
p2   facet_wrap2(vars(year), strip = strip_vanilla(clip = "on"))  
  ggtitle('clip = "on"')

plot of chunk unnamed-chunk-16

代码语言:javascript复制
p2   facet_wrap2(vars(year), strip = strip_vanilla(clip = "off"))  
  ggtitle('clip = "off"')

plot of chunk unnamed-chunk-16

代码语言:javascript复制
df <- data.frame(
  long = paste("A long title that is going to make then",
               "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

代码语言:javascript复制
p2   facet_grid2(long   short ~ ., switch = "y",
                strip = strip_vanilla(size = "constant"))  
  ggtitle('size = "constant"')

plot of chunk unnamed-chunk-17

代码语言:javascript复制
p2   facet_grid2(long   short ~ ., switch = "y",
                strip = strip_vanilla(size = "variable"))  
  ggtitle('size = "variable"')

plot of chunk unnamed-chunk-18

代码语言:javascript复制
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(0, 90)),
     by_layer_y = FALSE
)

p   facet_grid2(class ~ drv   year, strip = ridiculous_strips)

plot of chunk unnamed-chunk-19

代码语言:javascript复制
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(NULL, NULL, NULL, element_text(colour = "white"))
   )
)

plot of chunk unnamed-chunk-20

代码语言:javascript复制
p   facet_wrap2(
  vars(cyl, drv), ncol = 4,
  strip = strip_nested(bleed = FALSE)
)  
  ggtitle('bleed = FALSE')

plot of chunk unnamed-chunk-21

代码语言:javascript复制
p   facet_grid2(
  cols = vars("Outer label", "Inner label"),
  switch = "x", strip = strip_vanilla()
)  
  ggtitle("strip_vanilla()")

plot of chunk unnamed-chunk-22

代码语言:javascript复制
p   facet_grid2(
  cols = vars("Outer label", "Inner label"),
  switch = "x", strip = strip_nested()
)  
  ggtitle("strip_nested()")

plot of chunk unnamed-chunk-22

以上就是今天的内容,希望对你有帮助哦!

0 人点赞