强制facet_wrap填充底部行(并在顶部行中留下任何“间隙”) [英] Force facet_wrap to fill bottom row (and leave any "gaps" in the top row)

查看:154
本文介绍了强制facet_wrap填充底部行(并在顶部行中留下任何“间隙”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想强制facet_wrap从左上角填充,但总是完全填满最下面一行。 (即从左上角加上任何水平偏移量来完全填充底部行)。

  library(ggplot2 )数据帧(x = runif(n),y = rnorm(n),label = sample(字母[1:7],...,nf)。 )
$ d $ $ label.rev< - factor(df $ label,levels = sort(unique(df $ label),decrease = TRUE))
$ b $ (b)<-ggplot(df,aes(x = x,y = y))+ geom_point()
p1 <-p + facet_wrap(〜label,ncol = 3)
p2 < p + facet_wrap(〜label.rev,ncol = 3,as.table = FALSE)

p3

p1:我很高兴从标签的左到右,从上到下排序, d缺口位于左上角,而不是右下角。



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



p3:类似于p2;尝试修复标签订单,并失败。



我想要按以下顺序进行订购:

  _ _ A 
BCD
EFG

...这样整个底部的行就有了轴,因为我认为它会比默认的好。

解决方案



<$ (n),y = r norm(n),标签=(n),其中,示例(字母[1:7],
size = n,replace = TRUE),stringsAsFactors = TRUE)
#遵循@Didzis的建议(稍作修改)
df $ label。新的< - 因子(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)





编辑(从@baptiste):



从上一个解决方案开始,从gtable中移除grobs会更容易,

  g = ggplotGrob(p)
##删除空面板
g $ grobs [names(g $ grobs)%in%c(panel1,panel2,strip_t.1,strip_t.2 )] = NULL
##将它们从布局中移除
g $ layout = g $ layout [!(g $ layout $ name%in%c(panel-1,panel-2 ,
strip_t-1,strip_t-2)),]
##将轴靠近面板
g $ layout [g $ layout $ name ==axis_l-1 ,c(l,r)] = c(9,9)
grid.newpage()
grid.draw(g)


I'd like to force facet_wrap to fill from the top-left, but in such a way as to always fully fill the bottom row. (That is, from the top-left plus whatever horizontal offset is required to fully fill the bottom row.)

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: I'm happy with the left-to-right, top-to-bottom ordering of the labels, but I'd like the gap to be at the top left instead of the bottom right.

p2: Gap is now in the top row (top right rather than top left, unfortunately), but the label order is wrong.

p3: Similar to p2; attempts to fix the label order, and fails.

I'd like the facets to be ordered like this:

_ _ A
B C D
E F G

...so that there are axes along the entire bottom row, and because I think it would look better than the default. How can I do this?

解决方案

Would this fix suffice?

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)


EDIT (from @baptiste):

Starting from the last solution, it's easier to remove the grobs from the gtable,

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)

这篇关于强制facet_wrap填充底部行(并在顶部行中留下任何“间隙”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆