强制 facet_wrap 填充底行(并在顶部行留下任何“间隙”)

9

我希望强制 facet_wrap 从左上角开始填充,但要以一种始终完全填充底行的方式进行。也就是说,从左上角开始加上必要的水平偏移量,以完全填充底部行。

library(ggplot2)

n <- 1000
df <- data.frame(x=runif(n), y=rnorm(n), label=sample(letters[1:7], size=n, replace=TRUE))
df$label.rev <- factor(df$label, levels=sort(unique(df$label), decreasing=TRUE))

p <- ggplot(df, aes(x=x, y=y)) + geom_point()
p1 <- p + facet_wrap(~ label, ncol=3)
p2 <- p + facet_wrap(~ label, ncol=3, as.table=FALSE)
p3 <- p + facet_wrap(~ label.rev, ncol=3, as.table=FALSE)

p1: 我很满意标签从左到右、从上到下的顺序,但我想把间隙放在左上角而不是右下角。

p2: 间隙现在在第一行(不幸的是在右上而不是左上),但标签顺序是错误的。

p3: 类似于p2;尝试修复标签顺序,但失败了。

我希望这些方面按照以下顺序排序:

_ _ A
B C D
E F G

我想让整个底部行都有坐标轴,因为我认为这比默认设置更美观。我该如何做到这一点?

1个回答

9
这个修复够用吗?
library(ggplot2)
n <- 1000
df <- data.frame(x = runif(n), y=rnorm(n), label = sample(letters[1:7], 
                 size = n, replace = TRUE), stringsAsFactors=TRUE)
# following @Didzis' suggestion (with some minor changes)
df$label.new <- factor(df$label, levels=sort(c(""," ",levels(df$label))))
p <- ggplot(df, aes(x=x, y=y)) + geom_point() + 
         facet_wrap(~ label.new, ncol=3,drop=FALSE)

从最后一个解决方案开始,更容易从gtable中删除grobs。
g = ggplotGrob(p)
## remove empty panels
g$grobs[names(g$grobs) %in% c("panel1", "panel2", "strip_t.1", "strip_t.2")] = NULL
## remove them from the layout
g$layout = g$layout[!(g$layout$name %in% c("panel-1", "panel-2", 
                                                "strip_t-1", "strip_t-2")),]
## move axis closer to panel
g$layout[g$layout$name == "axis_l-1", c("l", "r")] = c(9,9)
grid.newpage()
grid.draw(g)

enter image description here


2
不需要颠倒标签也可以实现相同效果。只需在原有的 levels 之前添加 df$label.new<-factor(df$label, levels=c(" "," ",levels(df$label))),然后再加上 p+facet_wrap(~ label.new, ncol=3,drop=FALSE) - Didzis Elferts
如果可能的话,我想要避免那些空面板。它们能否被设置为不可见?是否可以控制单个面板的样式? - Adrian
在我的机器上,行df$label.new <- factor(df$label, levels=sort(c(""," ",unique(df$label))))的效果不如预期--我的df$label.new全都是NA。我们是否运行不同版本的R?我使用的是3.0.1(2013-05-16)--“Good Sport”。 - Adrian
我突然想到你的.Rprofile文件中可能设置了stringsAsFactors=FALSE,这就解释了为什么我不得不使用levels(df$label)而不是unique()才能使它正常工作。 - baptiste
1
@fdetsch,请将“strip_t.1”和“strip_t.2”替换为“strip_t1”和“strip_t2”。 - Sandy Muspratt
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接