I have a data frame with 5 groups. I want to facet my data by group using ggplot. I need the panels to be ordered from right to left, and not from left to right (the default).
我有一个5组的数据框架。我想使用ggplot按组对数据进行facet。我需要从右到左排列面板,而不是从左到右(默认)。
df <- data.frame(group=letters[1:5],
x=1:5,
y=1:5)
ggplot(df, aes(x,y)) +
geom_point(size=3) +
facet_wrap(~group)
By default the panels are ordered from left to right, so that group 'a' appears at the top-left of the graph. The panel at the bottom-right is empty (as there are only 5 groups). I want the panels to appear from right to left. That is, 'a' should appear at the top-right and the panel at the left-bottom should be empty. Any ideas?
默认情况下,面板是从左到右排列的,因此组“a”出现在图表的左上角。右下角的面板是空的(因为只有5个组)。我希望面板从右到左。也就是说,a应该出现在右上角,而左下角的面板应该是空的。什么好主意吗?
Note: the question is not about the labels of the panels. It is also not about re-ordering the group with factor(). I looked at the facet_wrap() help, but the only options are the 'switch' and 'strip.position' arguments that deal with labels. The 'dir' argument allows vertical or horizontal position, which is not what I am looking for.
注意:问题不是关于面板的标签。它也不是用factor()重新排序组。我查看了facet_wrap()帮助,但惟一的选项是“switch”和“strip”。定位处理标签的论点。“dir”参数允许垂直或水平位置,这不是我要查找的。
Thanks in advance
谢谢提前
1 个解决方案
#1
3
Here's a solution that's somewhat hacky, because it requires manual positioning. We create a plotting function and then plot the first row of plots (rows 1 to 3 of df
and the second row of plots (rows 4 and 5 of df
) separately. We lay them out using grid.arrange
, but we add a blank plot with just the y-axis at the left end of the second row to create the blank space.
这是一个有点陈腐的解决方案,因为它需要手动定位。我们创建一个绘图函数,然后分别绘制第一行图(df的第1至3行和第2行图(df的第4和5行)。我们用网格来布置它们。排列,但是我们在第二行左边添加一个只有y轴的空白图来创建空白空间。
The manual adjustment of the widths
argument is necessary to get the blank plot to take up just the right amount of space so that the other two plots line up vertically with the plots above.
需要手动调整宽度参数,以使空白图占用适当的空间,以便其他两个图与上面的图垂直排列。
library(gridExtra)
library(grid)
pf = function(data, xrng=range(df$x), yrng=range(df$y)) {
ggplot(data, aes(x,y)) +
geom_point(size=3) +
facet_wrap(~ factor(group, rev(group))) +
scale_y_continuous(limits=yrng) +
scale_x_continuous(limits=xrng)
}
grid.arrange(pf(df[1:3,]),
arrangeGrob(pf(data.frame(x=-10,y=-10, group="x")) +
theme(panel.border=element_blank(),
panel.background=element_blank(),
strip.background=element_rect(colour=NA, fill=NA),
strip.text=element_text(colour=NA),
axis.text.x=element_text(colour=NA),
axis.title.x=element_text(colour=NA),
axis.ticks.x=element_blank(),
axis.title.y=element_text(angle=90, vjust=0.5),
axis.text.y=element_text(angle=0),
axis.ticks.y=element_line()),
pf(df[4:5,]) + theme(axis.text.y=element_blank(),
axis.title.y=element_blank(),
axis.ticks.y=element_blank()) ,
widths=c(1.12,2)),
ncol=1)
#1
3
Here's a solution that's somewhat hacky, because it requires manual positioning. We create a plotting function and then plot the first row of plots (rows 1 to 3 of df
and the second row of plots (rows 4 and 5 of df
) separately. We lay them out using grid.arrange
, but we add a blank plot with just the y-axis at the left end of the second row to create the blank space.
这是一个有点陈腐的解决方案,因为它需要手动定位。我们创建一个绘图函数,然后分别绘制第一行图(df的第1至3行和第2行图(df的第4和5行)。我们用网格来布置它们。排列,但是我们在第二行左边添加一个只有y轴的空白图来创建空白空间。
The manual adjustment of the widths
argument is necessary to get the blank plot to take up just the right amount of space so that the other two plots line up vertically with the plots above.
需要手动调整宽度参数,以使空白图占用适当的空间,以便其他两个图与上面的图垂直排列。
library(gridExtra)
library(grid)
pf = function(data, xrng=range(df$x), yrng=range(df$y)) {
ggplot(data, aes(x,y)) +
geom_point(size=3) +
facet_wrap(~ factor(group, rev(group))) +
scale_y_continuous(limits=yrng) +
scale_x_continuous(limits=xrng)
}
grid.arrange(pf(df[1:3,]),
arrangeGrob(pf(data.frame(x=-10,y=-10, group="x")) +
theme(panel.border=element_blank(),
panel.background=element_blank(),
strip.background=element_rect(colour=NA, fill=NA),
strip.text=element_text(colour=NA),
axis.text.x=element_text(colour=NA),
axis.title.x=element_text(colour=NA),
axis.ticks.x=element_blank(),
axis.title.y=element_text(angle=90, vjust=0.5),
axis.text.y=element_text(angle=0),
axis.ticks.y=element_line()),
pf(df[4:5,]) + theme(axis.text.y=element_blank(),
axis.title.y=element_blank(),
axis.ticks.y=element_blank()) ,
widths=c(1.12,2)),
ncol=1)