【问题标题】:How to remove empty facet with ggplot?如何用ggplot删除空白面?
【发布时间】:2018-08-07 10:57:04
【问题描述】:

我正在使用 facet_grid 用 ggplot 做 geom_point,但是我的 facet 是空的,我不知道如何删除它们或如何构造我的数据以使其没有空 facet?

这是我的数据示例:

data = data.frame(
  F1=c(0.69, 0.59, 0.6 , 0.52, 0.56, 0.58, 0.52, 0.53, 0.41, 0.57,
       0.54, 0.38, 0.48, 0.31, 0.35,
       0.43, 0.36, 0.38, 0.23, 0.48, 0.55, 0.48, 0.49, 0.46, 0.49,
       0.52, 0.48, 0.48, 0.35, 0.5 ,
       0.51, 0.58, 0.51, 0.59, 0.51, 0.57, 0.5 , 0.59, 0.47, 0.51,
       0.61, 0.58, 0.61, 0.59, 0.61, 0.67, 0.6 , 0.59, 0.47, 0.61,
       0.61, 0.52, 0.53, 0.60,0.62, 0.53, 0.62, 0.63, 0.24, 0.38),
  F2 = c(0.01, 0.01, 0.02, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01,
         0.42, 0.35, 0.43, 0.31, 0.34, 0.41, 0.34, 0.34, 0.42, 0.39, 0.44, 0.43,
         0.49, 0.43, 0.42, 0.53, 0.41, 0.42, 0.50, 0.40 ,
         0.53, 0.58, 0.57, 0.55, 0.51, 0.65, 0.51, 0.52, 0.62, 0.49,
         0.63, 0.68, 0.67, 0.66, 0.61, 0.66, 0.61, 0.62, 0.62, 0.49,
         0.84, 0.65, 0.69, 0.56, 0.72, 0.61, 0.73, 0.68, 0.72, 0.72),
  s1= c(rep(c("a"),10),
        rep(c("b"),10),
        rep(c("c"),10),
        rep(c("d"),10),
        rep(c("f"),10),
        rep(c("g"),10)),
  s2= c(rep(c("g1"),20),
        rep(c("g2"),40)),
  M=rep(c("M1",
          "M2",  
          "M3",    
          "M4",    
          "M5",   
          "M6",         
          "M7", 
          "M8",
          "M9",
          "M10"),6))

我的代码:

ggplot (data, aes (x = F1, y = F2, shape = M)) +
  facet_grid(s2 ~ s1) + scale_shape_manual(values=c(7,13,23,0,8,1,15,2,17,3,25))+
  geom_point () 

我明白了:

情节预期:

【问题讨论】:

  • 使用facet_wrap
  • 当然我试过 facet_wrap(s2 ~ s1) 但它没有给我想要的结果!
  • 我试过你的代码,但我没有在它的右侧得到额外的文本。我也想在同一行只有两个方面,右边有额外的文字
  • This post on remove grobs 可能会帮助您删除空面板,但您可能想要移动剩余的面板。或者,您可以使用生成带有一些子集的每个单独的图和包grid.extra 在同一页面上绘制结果图。

标签: r ggplot2 facet-wrap facet-grid


【解决方案1】:

我不知道为什么 facet_wrap 不起作用

下面的代码产生你想要的

p <- ggplot (data, aes (x = F1, y = F2, shape = M))
p <- p + geom_point () 
p <- p +  facet_wrap(s2 ~ s1,ncol=2) + scale_shape_manual(values=c(7,13,23,0,8,1,15,2,17,3,25))
p

【讨论】:

  • 感谢您的建议,您的代码在 top 处添加了文本 g1 和 g2 ,我希望文本 g1 和 g2 在 right 如我帖子中的照片所示!
  • @Axeman 的解决方案可能会更好,如果您真的想要显示的显示。否则,为节省空间而调用 facet_wrap 的 labeller = label_wrap_gen(multi_line=FALSE) 参数
  • 感谢您的解决方案,但我仍在尝试使用以下功能:gtable_add_colsgtable_add_grob 以获取我的绘图
【解决方案2】:

如果您真的需要在两侧创建带有条形标签的示例输出,则需要创建三个图,例如:

p3 <- ggplot(subset(data, s1 %in% c('f', 'g')), aes(F1, F2, shape = M)) + 
  geom_point() + 
  facet_grid(s2 ~ s1) +
  scale_shape_manual(values = c(7,13,23,0,8,1,15,2,17,3,25))
p1 <- p3 %+% subset(data, s1 %in% c('a', 'b')) +
  theme(axis.text.x = element_blank(), axis.ticks.x = element_blank(), 
        axis.title.x = element_blank(), legend.position = 'none')
p2 <- p1 %+% subset(data, s1 %in% c('c', 'd'))

cowplot::plot_grid(p1, p2, p3, nrow = 3, align = 'v', axis = 'lr', rel_heights = c(1, 1, 1.2))

但我通常会选择@Boidot 提供的更简单的包装解决方案。

【讨论】:

  • 我似乎无法附加图片,imgur 似乎有问题。
  • y 轴可以有一个标题吗?
  • 我找到了一种使用以下函数进行绘图的方法:gtable_add_cols, gtable_add_grob .. 但我在添加列的大小方面遇到了问题 [链接] (stackoverflow.com/questions/51728292/…)
【解决方案3】:

我为我的问题找到了解决方案,我想与您分享:

p <- qplot(data=data, x=F1, y=F2)+facet_wrap( ~ s1,ncol = 2)
z <- ggplotGrob(p)
gtable_show_layout(z)
z <- gtable_add_cols(z, unit(0.08, 'null'), 11)
gtable_show_layout(z)
z <- gtable_add_grob(z,
                     list(rectGrob(gp = gpar(col=NA, fill = gray(0.85), size = 0.5,face="bold")),  
                          textGrob("g1", 
                                   rot = -90, gp = gpar(col = gray(0),fontsize=12,fontface = 'bold'))),
                     7, 12,9,12, name = paste(runif(2)))
gtable_show_layout(z)
z <- gtable_add_grob(z,
                     list(rectGrob(gp = gpar(col=NA, fill = gray(0.85), size = 0.5,face="bold")),  
                          textGrob("g2", 
                                   rot = -90, gp = gpar(col = gray(0),fontsize=12,fontface = 'bold'))),
                     12, 12,18,12, name = paste(runif(2)))

z <- gtable_add_cols(z, unit(1/6, "line"),11)
grid.newpage()
grid.draw(z)

我希望我的解决方案对其他人有用!

【讨论】:

    【解决方案4】:

    那个方法怎么样?

    data %>% 
      ggplot(aes(F1, F2, shape = M, color = s2)) +
      geom_point() +
      facet_grid(~ s1) + 
      scale_shape_manual(values=c(7,13,23,0,8,1,15,2,17,3,25))
    

    情节是:

    【讨论】:

    • 我在 filter_impl(.data, quo) 中发现了这个错误:结果的长度必须是 4,而不是 60!我正在尝试使用 qplot(data=data, x=F1, y=F2)+facet_wrap( ~ s1,ncol = 2) 并使用 gtable_add_cols 添加列...但我还没有成功!谢谢你帮助我......如果你知道我如何使用这些功能......
    • 抱歉,我的数据集有误,您必须将 df 替换为 data。我编辑了我的答案。希望有帮助。
    • 对不起,@Axeman 但那基本上是错误的。我在答案中添加了图像。我建议你先检查输出。
    • 您的绘图也不需要两个geom_point 层。只需ggplot(data, aes(F1, F2, shape = M, color = s2)) + geom_point() + ... 即可。
    猜你喜欢
    • 1970-01-01
    • 2016-05-22
    • 2017-12-27
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多