R语言 如何使用ggplot删除空面?

6gpjuf90  于 2023-05-04  发布在  其他
关注(0)|答案(5)|浏览(177)

我正在使用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 ()

我明白了

预期图:

3z6pesqy

3z6pesqy1#

我不知道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
nbysray5

nbysray52#

如果你真的需要在两侧创建带标签的示例输出,你需要创建三个图,类似于:

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的更简单的 Package 解决方案。

dphi5xsq

dphi5xsq3#

我找到了一个解决问题的方法,我想和你分享:

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)

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

osh3o9ms

osh3o9ms4#

那方法怎么样?

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))

plot是:

tzdcorbm

tzdcorbm5#

另一种选择是使用ggh4x包和facet_manual函数来创建基于s1和s2列的自定义设计布局,如下所示:

library(ggplot2)
library(ggh4x)

design = "
AB
CD
EF"

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

创建于2023-04-28带有reprex v2.0.2

相关问题