如何在2-way facet_grid中删除空面板?

huangapple go评论104阅读模式
英文:

How to remove empty panel in 2-way facet_grid?

问题

我想要移除 ggplot 2-way facet_grid 中的整个面板。我从这里这个问题中获取了一些启示。这是带有示例数据的代码

  1. 库(ggplot2)
  2. 库(ggh4x)
  3. df <- 数据框架(x=rnorm(80),y=rnorm(80),
  4. col=rep(c(" ", "B","C"," ","B","C","B","C"),each=10),
  5. row=rep(c("a","a","a","b","b","b","c","c"),each=10))
  6. ggplot(data=df,aes(x=x,y=y))+
  7. geom_point()+
  8. facet_grid2(row~col, scales = "free", independent = "all")

我想移除左侧面板。应该如何操作?

英文:

I want to remove the whole panel from ggplot 2-way facet_grid. I have taken some lead from this question. Here is the code with example data

  1. library(ggplot2)
  2. library(ggh4x)
  3. df <- data.frame(x=rnorm(80),y=rnorm(80),
  4. col=rep(c(" ", "B","C"," ","B","C","B","C"),each=10),
  5. row=rep(c("a","a","a","b","b","b","c","c"),each=10))
  6. ggplot(data=df,aes(x=x,y=y))+
  7. geom_point()+
  8. facet_grid2(row~col, scales = "free", independent = "all")

如何在2-way facet_grid中删除空面板?

I want to remove the left panel. How can it be done?

答案1

得分: 3

以下是您要翻译的内容:

根据您的评论,事先对数据集进行子集处理对您不是一个选项,所以这里是一种后期处理方法:

  1. # 创建绘图
  2. p <- ggplot(data=df,aes(x=x,y=y))+
  3. geom_point()+
  4. facet_grid2(row~col, scales = "free", independent = "all")
  5. # 转换为grob对象
  6. gp <- ggplotGrob(p)
  7. # 检查要删除的gtable列
  8. # (在此示例中,我们要删除列4到5的内容)
  9. gp # 在控制台中查看布局
  10. gtable::gtable_show_layout(gp) # 在查看器中查看布局
  11. # 删除它们
  12. gp <- gp %>%
  13. cowplot::gtable_remove_grobs(names = gp$layout %>%
  14. filter(r %in% 4:5) %>%
  15. pull(name)) %>%
  16. cowplot::gtable_squash_cols(cols = 4:5)
  17. # 绘制结果
  18. plot(gp)

如何在2-way facet_grid中删除空面板?

数据:

  1. set.seed(123)
  2. df <- data.frame(x=rnorm(80),y=rnorm(80),
  3. col=rep(c(" ", "B","C"," ","B","C","B","C"),each=10),
  4. row=rep(c("a","a","a","b","b","b","c","c"),each=10))
  1. <details>
  2. <summary>英文:</summary>
  3. Noted from your comment that subsetting the dataset beforehand isn&#39;t an option for you, so here&#39;s a post-production approach:

create plot

p <- ggplot(data=df,aes(x=x,y=y))+
geom_point()+
facet_grid2(row~col, scales = "free", independent = "all")

convert to grob

gp <- ggplotGrob(p)

check which are the columns of the gtable to strip out

(for the example, we want to strip out content from cols 4-5)

gp # examine layout in console
gtable::gtable_show_layout(gp) # examine layout in viewer

strip them out

gp <- gp %>%
cowplot::gtable_remove_grobs(names = gp$layout %>%
filter(r %in% 4:5) %>%
pull(name)) %>%
cowplot::gtable_squash_cols(cols = 4:5)

plot the result

plot(gp)

  1. [![result][1]][1]
  2. data:

set.seed(123)
df <- data.frame(x=rnorm(80),y=rnorm(80),
col=rep(c(" ", "B","C"," ","B","C","B","C"),each=10),
row=rep(c("a","a","a","b","b","b","c","c"),each=10))

  1. [1]: https://i.stack.imgur.com/eSXoi.png
  2. </details>
  3. # 答案2
  4. **得分**: 2
  5. One way is to use `subset()`:
  6. ```R
  7. library(ggplot2)
  8. library(ggh4x)
  9. df %>%
  10. subset(!(row == "a" & col == " ")) %>%
  11. ggplot(aes(x=x,y=y)) +
  12. geom_point() +
  13. facet_grid2(row~col, scales = "free", independent = "all")

如何在2-way facet_grid中删除空面板?

英文:

One way is to use subset():

  1. library(ggplot2)
  2. library(ggh4x)
  3. df %&gt;%
  4. subset(!(row == &quot;a&quot; &amp; col == &quot; &quot;)) %&gt;%
  5. ggplot(aes(x=x,y=y)) +
  6. geom_point() +
  7. facet_grid2(row~col, scales = &quot;free&quot;, independent = &quot;all&quot;)

如何在2-way facet_grid中删除空面板?

huangapple
  • 本文由 发表于 2023年5月13日 16:53:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定