我想强制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)
Run Code Online (Sandbox Code Playgroud)
p1:我很满意标签的从左到右,从上到下排序,但我希望间隙位于左上角而不是右下角.
p2:Gap现在位于顶行(不幸的是,右上角而不是左上角),但标签顺序错误.
p3:与p2相似; 尝试修复标签订单,但失败了.
我想要像这样订购方面:
_ _ A
B C D
E F G
Run Code Online (Sandbox Code Playgroud)
...所以沿整个底行都有轴,因为我觉得它看起来比默认更好.我怎样才能做到这一点?
这会解决这个问题吗?
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)
Run Code Online (Sandbox Code Playgroud)

从最后一个解决方案开始,更容易从gtable中删除grob,
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)
Run Code Online (Sandbox Code Playgroud)
