Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 需要:facet_网格-facet_包裹混合解决方案_R_Ggplot2_Facet_Facet Wrap - Fatal编程技术网

R 需要:facet_网格-facet_包裹混合解决方案

R 需要:facet_网格-facet_包裹混合解决方案,r,ggplot2,facet,facet-wrap,R,Ggplot2,Facet,Facet Wrap,我正在尝试在facet_grid和facet_wrap之间生成一种“混合”布局的绘图: 例如: library(reshape2) library(ggplot2) data(diamonds) # sample and reshape diamond dataset diamonds_l <- melt(diamonds[sample(1:nrow(diamonds), 200), c("cut", "depth", "tab

我正在尝试在facet_grid和facet_wrap之间生成一种“混合”布局的绘图:

例如:

library(reshape2)
library(ggplot2)

data(diamonds)

# sample and reshape diamond dataset
diamonds_l <- melt(diamonds[sample(1:nrow(diamonds), 200), 
                            c("cut", "depth", "table", "price", "x")],
                   id.vars = c("cut","x"))

但是,我更喜欢facet_网格设计(每列/每行只有一个标题) 但是scales=“free_x”在此布局中不起作用

ggplot( diamonds_l, aes( x = value, y = x, colour= cut))+
  geom_point( ) +
  facet_grid(variable ~  cut, scales = "free_x") 

它在这里工作,但这不是我想要的安排(行的质量)

我理解为什么它不起作用,但我想知道是否有工作可以做

谢谢


费边

经过一番挖掘,我成功地完成了这项任务。从(@baptiste,@Roland)和(@Sandy Muspratt)借用的代码片段。看起来很吓人,但我基本上是通过低级操作从你的第一个情节中删除/重命名文本条

p <- ggplot( diamonds_l, aes( x = value, y = x, colour= cut))+
  geom_point( ) +
  facet_wrap( variable ~ cut, scales = "free_x", nrow = 3) 

library(gridExtra)
gt <- ggplotGrob(p)
panels <- grep("panel", gt$layout$name)
top <- unique(gt$layout$t[panels])
gt <- gt[-(top[-1]-1), ]

gg <- gt$grobs      
strips <- grep("strip_t", names(gg))
labels <- levels(diamonds_l$cut)
for(ii in seq_along(labels))  {
  modgrob <- getGrob(gg[[strips[ii]]], "strip.text", 
                     grep=TRUE, global=TRUE)
  gg[[strips[ii]]]$children[[modgrob$name]] <- editGrob(modgrob,label=labels[ii])
}
gt$grobs <- gg
grid.newpage()
grid.draw(gt)

p谢谢,真是太好了!我看到了一些其他的地方,你可以通过低水平的操作移除这些条带,但我希望有一个更简单的解决方案。。此外,除了删除面板列标签外,是否可以像在facet_grid中那样添加行标签?我把问题留长一点,但如果没有人提出更简单的(这似乎不可能)好的答案,我会接受你的答案。是的,这可能是可能的,尽管添加东西通常比删除东西容易:)轮廓是1)从
facet\u网格中提取行带,2)将它们插入上面绘图的grob树中。不过,恐怕我的网格不够酷。你知道吗,如果没有其他人出现,你可以问一个后续问题,描述我们的现状和拟议的大纲。用
grid
gridExtra
标记它,也许我们很幸运。如果你这样做,请给我一个评论,否则我可能会错过它。
ggplot( diamonds_l, aes( x = value, y = x, colour= cut))+
  geom_point( ) +
  facet_grid(cut ~ variable, scales = "free_x")
p <- ggplot( diamonds_l, aes( x = value, y = x, colour= cut))+
  geom_point( ) +
  facet_wrap( variable ~ cut, scales = "free_x", nrow = 3) 

library(gridExtra)
gt <- ggplotGrob(p)
panels <- grep("panel", gt$layout$name)
top <- unique(gt$layout$t[panels])
gt <- gt[-(top[-1]-1), ]

gg <- gt$grobs      
strips <- grep("strip_t", names(gg))
labels <- levels(diamonds_l$cut)
for(ii in seq_along(labels))  {
  modgrob <- getGrob(gg[[strips[ii]]], "strip.text", 
                     grep=TRUE, global=TRUE)
  gg[[strips[ii]]]$children[[modgrob$name]] <- editGrob(modgrob,label=labels[ii])
}
gt$grobs <- gg
grid.newpage()
grid.draw(gt)