需要:facet_grid - facet_wrap 混合解决方案
wanted: facet_grid - facet_wrap hybrid solution
我正在尝试制作一个在 facet_grid 和 facet_wrap 之间具有某种 "hybrid" 布局的图:
示例:
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"))
这是我想要的情节安排(列和深度的质量,table,行的价格)
ggplot( diamonds_l, aes( x = value, y = x, colour= cut))+
geom_point( ) +
facet_wrap( variable ~ cut, scales = "free_x", nrow=3)
不过,我更喜欢 facet_grid 设计(每列/行只有一个 header)
但是 scales = "free_x" 在此布局中不起作用
ggplot( diamonds_l, aes( x = value, y = x, colour= cut))+
geom_point( ) +
facet_grid(variable ~ cut, scales = "free_x")
它在这里工作,但那不是我想要的安排(质量作为行)
ggplot( diamonds_l, aes( x = value, y = x, colour= cut))+
geom_point( ) +
facet_grid(cut ~ variable, scales = "free_x")
我明白为什么它不起作用,但我想知道是否有 work-around?
谢谢!
法比安
经过一番挖掘,我设法成功了。从 here (@baptiste, @Roland) and here (@Sandy Muspratt) 借来的代码片段。看起来很可怕,但我基本上是通过低级操作从你的第一个情节中 removing/renaming 文本条。
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)
我正在尝试制作一个在 facet_grid 和 facet_wrap 之间具有某种 "hybrid" 布局的图:
示例:
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"))
这是我想要的情节安排(列和深度的质量,table,行的价格)
ggplot( diamonds_l, aes( x = value, y = x, colour= cut))+
geom_point( ) +
facet_wrap( variable ~ cut, scales = "free_x", nrow=3)
不过,我更喜欢 facet_grid 设计(每列/行只有一个 header) 但是 scales = "free_x" 在此布局中不起作用
ggplot( diamonds_l, aes( x = value, y = x, colour= cut))+
geom_point( ) +
facet_grid(variable ~ cut, scales = "free_x")
它在这里工作,但那不是我想要的安排(质量作为行)
ggplot( diamonds_l, aes( x = value, y = x, colour= cut))+
geom_point( ) +
facet_grid(cut ~ variable, scales = "free_x")
我明白为什么它不起作用,但我想知道是否有 work-around?
谢谢!
法比安
经过一番挖掘,我设法成功了。从 here (@baptiste, @Roland) and here (@Sandy Muspratt) 借来的代码片段。看起来很可怕,但我基本上是通过低级操作从你的第一个情节中 removing/renaming 文本条。
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)