在ggplot中从右到左订购面板 [英] Order the panels from right to left in ggplot

查看:59
本文介绍了在ggplot中从右到左订购面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含5组的数据框.我想使用ggplot按组对数据进行分面.我需要将面板从右到左排序,而不是从左到右排序(默认).

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).

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)

默认情况下,面板按从左到右的顺序排列,因此组"a"出现在图形的左上角.右下角的面板为空(因为只有5组).我希望面板从右到左显示.也就是说,"a"应出现在右上角,而左下的面板应为空.有什么想法吗?

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?

注意:问题与面板的标签无关.这也不是要通过factor()对组进行重新排序.我查看了facet_wrap()帮助,但是唯一的选项是处理标签的'switch'和'strip.position'参数."dir"参数允许垂直或水平位置,这不是我想要的.

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.

预先感谢

推荐答案

这里的解决方案有些怪异,因为它需要手动定位.我们创建一个绘图函数,然后分别绘制第一行绘图( df 的第1至3行和第二行绘图( df 的第4行和第5行).我们使用 grid.arrange 对其进行布局,但是我们在第二行的左端添加了一个仅带有y轴的空白图来创建空白.

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.

必须手动调整 width 参数,以使空白图占据正确的空间,以便其他两个图与上面的图垂直对齐.

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)

这篇关于在ggplot中从右到左订购面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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