【问题标题】:ggplot: How to label facets when using `facet_zoom()` from `ggforce` packge?ggplot:使用 ggforce 包中的 facet_zoom() 时如何标记构面?
【发布时间】:2021-12-30 02:34:48
【问题描述】:

当使用来自{ggforce} packge 的facet_zoom() 时,有没有办法自己标记构面?

例如,我可以制作如下图:

library(ggplot2)
library(ggforce)

ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
  geom_point() +
  facet_zoom(x = Species == 'versicolor') +
  labs(caption = "Figure 1. a) entire data range; b) zoom in on versicolor") +
  theme_bw() +
  theme(plot.caption = element_text(hjust = 0))

reprex package (v2.0.1) 于 2021 年 11 月 19 日创建


但我如何才能真正标记这些方面?我想实现类似:

【问题讨论】:

    标签: r ggplot2 ggforce


    【解决方案1】:

    手动解决方案可以基于来自{cowplot} 包的draw_label()

    library(ggplot2)
    library(ggforce)
    library(cowplot)
    
    p <- 
      ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
      geom_point() +
      facet_zoom(x = Species == 'versicolor') +
      labs(caption = "Figure 1. a) entire data range; b) zoom in on versicolor") +
      theme_bw() +
      theme(plot.caption = element_text(hjust = 0))
    
    ggdraw(p) +
      draw_label("a)", color = "black", size = 15, x = 0.09, y = 0.95) +
      draw_label("b)", color = "black", size = 15, x = 0.09, y = 0.6)
    

    reprex package (v2.0.1) 于 2021 年 11 月 19 日创建


    但是,这远非最佳,因为标签的位置是绝对的,这意味着在 RStudio 中调整绘图的大小也会改变位置。最佳解决方案是将标签锚定到相对于构面的某个位置,例如:"upper-left",因此不受调整大小的影响。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2021-03-07
      • 1970-01-01
      相关资源
      最近更新 更多