使用Rpy2在Python中对ggplot2中的不同非facet图进行对齐

12

我正在使用python中的rpy2,按照@lgautier的建议,使用grid将两个不同的图形合并到网格布局中。顶部图形是密度图,底部图形是条形图:

iris = r('iris')
import pandas

# define layout
lt = grid.layout(2, 1)
vp = grid.viewport(layout = lt)
vp.push()

# first plot
vp_p = grid.viewport(**{'layout.pos.row': 1, 'layout.pos.col':1})
p1 = ggplot2.ggplot(iris) + \
    ggplot2.geom_density(aes_string(x="Sepal.Width",
                                    colour="Species")) + \
    ggplot2.facet_wrap(Formula("~ Species"))
p1.plot(vp = vp_p)

# second plot
mean_df = pandas.DataFrame({"Species": ["setosa", "virginica", "versicolor"],
                            "X": [10, 2, 30],
                            "Y": [5, 3, 4]})
mean_df = pandas.melt(mean_df, id_vars=["Species"])
r_mean_df = get_r_dataframe(mean_df)
p2 = ggplot2.ggplot(r_mean_df) + \
     ggplot2.geom_bar(aes_string(x="Species",
                                 y="value",
                                 group="variable",
                                 colour="variable"),
                      position=ggplot2.position_dodge(),
                      stat="identity")
vp_p = grid.viewport(**{'layout.pos.row': 2, 'layout.pos.col':1})
p2.plot(vp = vp_p)

我得到的结果接近于我想要的,但是图表不完全对齐(由我添加的箭头所示): enter image description here 我希望图表区域(而不是图例)完全匹配。如何实现?这里的差异不是很大,但是当您向下面的条形图添加条件或使它们成为具有position_dodge的分组条形图时,差异可能会变得非常大,并且图表不对齐。
标准的ggplot解决方案不能轻松地转换为rpy2: arrangegridExtra中似乎是 grid_arrange
>>> gridExtra = importr("gridExtra")
>>> gridExtra.grid_arrange
<SignatureTranslatedFunction - Python:0x430f518 / R:0x396f678>

ggplotGrobggplot2中不可访问,但可以通过以下方式访问:

>>> ggplot2.ggplot2.ggplotGrob

虽然我不知道如何访问grid::unit.pmax

>>> grid.unit
<bound method type.unit of <class 'rpy2.robjects.lib.grid.Unit'>>
>>> grid.unit("pmax")
Error in (function (x, units, data = NULL)  : 
argument "units" is missing, with no default
rpy2.rinterface.RRuntimeError: Error in (function (x, units, data = NULL)  : 
argument "units" is missing, with no default

因此,不清楚如何将标准的ggplot2解决方案翻译成rpy2。

编辑:正如其他人指出的那样,grid::unit.pmaxgrid.unit_pmax。然而,我仍然不知道如何在rpy2中访问grob对象的widths参数,这是必要的,以将图形的宽度设置为更宽的图形的宽度。我有:

gA = ggplot2.ggplot2.ggplotGrob(p1)
gB = ggplot2.ggplot2.ggplotGrob(p2)

g = importr("grid")
print "gA: ", gA
maxWidth = g.unit_pmax(gA.widths[2:5], gB.widths[2:5])

gA.widths 不是正确的语法。 grob 对象 gA 的输出为:

gA:  TableGrob (8 x 13) "layout": 17 grobs
    z         cells       name                                    grob
1   0 ( 1- 8, 1-13) background          rect[plot.background.rect.350]
2   1 ( 4- 4, 4- 4)    panel-1                gTree[panel-1.gTree.239]
3   2 ( 4- 4, 7- 7)    panel-2                gTree[panel-2.gTree.254]
4   3 ( 4- 4,10-10)    panel-3                gTree[panel-3.gTree.269]
5   4 ( 3- 3, 4- 4)  strip_t-1    absoluteGrob[strip.absoluteGrob.305]
6   5 ( 3- 3, 7- 7)  strip_t-2    absoluteGrob[strip.absoluteGrob.311]
7   6 ( 3- 3,10-10)  strip_t-3    absoluteGrob[strip.absoluteGrob.317]
8   7 ( 4- 4, 3- 3)   axis_l-1 absoluteGrob[axis-l-1.absoluteGrob.297]
9   8 ( 4- 4, 6- 6)   axis_l-2         zeroGrob[axis-l-2.zeroGrob.298]
10  9 ( 4- 4, 9- 9)   axis_l-3         zeroGrob[axis-l-3.zeroGrob.299]
11 10 ( 5- 5, 4- 4)   axis_b-1 absoluteGrob[axis-b-1.absoluteGrob.276]
12 11 ( 5- 5, 7- 7)   axis_b-2 absoluteGrob[axis-b-2.absoluteGrob.283]
13 12 ( 5- 5,10-10)   axis_b-3 absoluteGrob[axis-b-3.absoluteGrob.290]
14 13 ( 7- 7, 4-10)       xlab             text[axis.title.x.text.319]
15 14 ( 4- 4, 2- 2)       ylab             text[axis.title.y.text.321]
16 15 ( 4- 4,12-12)  guide-box                       gtable[guide-box]
17 16 ( 2- 2, 4-10)      title               text[plot.title.text.348]

更新:在访问宽度方面取得了一些进展,但仍无法翻译出解决方案。要设置 grobs 的宽度,我有以下方法:

# get grobs
gA = ggplot2.ggplot2.ggplotGrob(p1)
gB = ggplot2.ggplot2.ggplotGrob(p2)
g = importr("grid")
# get max width
maxWidth = g.unit_pmax(gA.rx2("widths")[2:5][0], gB.rx2("widths")[2:5][0])
print gA.rx2("widths")[2:5]
wA = gA.rx2("widths")[2:5]
wB = gB.rx2("widths")[2:5]
print "before: ", wA[0]
wA[0] = robj.ListVector(maxWidth)
print "After: ", wA[0]
print "before: ", wB[0]
wB[0] = robj.ListVector(maxWidth)
print "after:", wB[0]
gridExtra.grid_arrange(gA, gB, ncol=1)

它能运行但不能正常工作。输出结果如下:
[[1]]
[1] 0.740361111111111cm

[[2]]
[1] 1null

[[3]]
[1] 0.127cm


before:  [1] 0.740361111111111cm

After:  [1] max(0.740361111111111cm, sum(1grobwidth, 0.15cm+0.1cm))

before:  [1] sum(1grobwidth, 0.15cm+0.1cm)

after: [1] max(0.740361111111111cm, sum(1grobwidth, 0.15cm+0.1cm))

更新2:正如@baptiste所指出的那样,展示我试图在rpy2中复制的纯R版本会很有帮助。以下是纯R版本:

df <- data.frame(Species=c("setosa", "virginica", "versicolor"),X=c(1,2,3), Y=c(10,20,30))
p1 <- ggplot(iris) + geom_density(aes(x=Sepal.Width, colour=Species))
p2 <- ggplot(df) + geom_bar(aes(x=Species, y=X, colour=Species))
gA <- ggplotGrob(p1)
gB <- ggplotGrob(p2)
maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)
grid.arrange(gA, gB, ncol=1)

我认为这通常适用于ggplot2中具有不同facet的带有图例的两个面板,我想在rpy2中实现这一点。

更新3:通过逐个构建一个FloatVector元素来实现几乎可以工作:

maxWidth = []
for x, y in zip(gA.rx2("widths")[2:5], gB.rx2("widths")[2:5]):
    pmax = g.unit_pmax(x, y)
    print "PMAX: ", pmax
    val = pmax[1][0][0]
    print "VAL->", val
    maxWidth.append(val)
gA[gA.names.index("widths")][2:5] = robj.FloatVector(maxWidth)
gridExtra.grid_arrange(gA, gB, ncol=1)

然而,这会导致段错误/核心转储:
Error: VECTOR_ELT() can only be applied to a 'list', not a 'double'
*** longjmp causes uninitialized stack frame ***: python2.7 terminated
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x37)[0x7f83742e2817]
/lib/x86_64-linux-gnu/libc.so.6(+0x10a78d)[0x7f83742e278d]
/lib/x86_64-linux-gnu/libc.so.6(__longjmp_chk+0x33)[0x7f83742e26f3]
...
7f837591e000-7f8375925000 r--s 00000000 fc:00 1977264                    /usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache
7f8375926000-7f8375927000 rwxp 00000000 00:00 0 
7f8375927000-7f8375929000 rw-p 00000000 00:00 0 
7f8375929000-7f837592a000 r--p 00022000 fc:00 917959                     /lib/x86_64-linux-gnu/ld-2.15.so
7f837592a000-7f837592c000 rw-p 00023000 fc:00 917959                     /lib/x86_64-linux-gnu/ld-2.15.so
7ffff4b96000-7ffff4bd6000 rw-p 00000000 00:00 0                          [stack]
7ffff4bff000-7ffff4c00000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)

更新: 赏金已经结束。感谢收到的答案,但是两个答案都没有使用rpy2,这是一个关于rpy2的问题,所以技术上来说这些答案不是相关主题。虽然像@baptiste指出的那样,在一般情况下可能没有解决这个问题的方法,但是有一个简单的R语言解决方案,问题就是如何将其翻译成rpy2。


7
将此标记为重复的人对于从R到Rpy2的代码翻译的细微差别没有任何欣赏。是的,有时翻译很容易,但有时它是反直觉的。这是一个关于rpy2的问题,不是一个简单的ggplot2问题。你只是无缘无故地干扰和阻止了问题和答案的过程。 - user248237
unit.pmax是一个与unit分开的函数(尽管两者都在grid包中)。在grid=importr("grid")之后,Python中是否有grid.unit.pmax可用?或者rpy是否对与S3方法调度无关的函数名称中的点进行了一些翻译? - Brian Diggs
2
@BrianDiggs 看起来 grid::unit.pmax 将变成 grid.unit_pmax - baptiste
@baptiste:但是看起来 wAwB 是否已经被正确更新了呢? - user248237
@user248237dfsf - 程序崩溃不应该发生。你是否在rpy2的问题跟踪器上报告了它? - lgautier
显示剩余5条评论
3个回答

6

当涉及到分面时,将两个图形对齐变得更加棘手。我不知道是否有一般的解决方案,即使在R中也是如此。考虑以下情况:

p1 <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + 
  facet_wrap(~ cyl, ncol=2,scales="free")
p2 <- p1 + facet_null() + aes(colour=am) + ylab("this\nis taller")

gridExtra::grid.arrange(p1, p2)

图片描述在这里输入

通过一些工作,您可以比较左轴和图例(可能存在于右侧或不存在)的宽度。

library(gtable)

# legend, if it exists, may be the second last item on the right, 
# unless it's not on the right side.
locate_guide <- function(g){
  right <- max(g$layout$r)
  gg <- subset(g$layout, (grepl("guide", g$layout$name) & r == right - 1L) | 
                 r == right)
  sort(gg$r)
}

compare_left <- function(g1, g2){

  w1 <- g1$widths[1:3]
  w2 <- g2$widths[1:3]
  unit.pmax(w1, w2)
}

align_lr <- function(g1, g2){

  # align the left side 
  left <- compare_left(g1, g2)
  g1$widths[1:3] <- g2$widths[1:3] <- left

  # now deal with the right side

  gl1 <- locate_guide(g1)
  gl2 <- locate_guide(g2)

  if(length(gl1) < length(gl2)){
    g1$widths[[gl1]] <- max(g1$widths[gl1], g2$widths[gl2[2]]) +
      g2$widths[gl2[1]]
  }
  if(length(gl2) < length(gl1)){
    g2$widths[[gl2]] <- max(g2$widths[gl2], g1$widths[gl1[2]]) +
      g1$widths[gl1[1]]
  }
  if(length(gl1) == length(gl2)){
    g1$widths[[gl1]] <-  g2$widths[[gl2]] <- unit.pmax(g1$widths[gl1], g2$widths[gl2])
  }

  grid.arrange(g1, g2)
}

align_lr(g1, g2)

请注意,我还没有测试其他情况; 我确信很容易出问题。从文档中我所理解的是,rpy2 提供了一种机制来使用任意一段 R 代码,因此转换应该不是问题。

但是人们经常在R中制作复杂的图形网格,那么一般的方法是什么?是否有一种方法可以使用facet_wrap和facet_grid在ggplot分面中实现我在帖子中制作的原始图形,以避免完全分离的网格?也许我只是不知道facetting的高级用法。 - user248237
关于你的例子,我有一个问题:如果你删除了“am”的图例,你能否在R中对齐它们? - user248237
1
另一个后续问题:在不同的数据子集上使用不同的几何类型的方法有多通用?这是否允许您使用geom_densitygeom_bar的组合创建原始帖子中所需的图形?https://dev59.com/zWsz5IYBdhLWcg3wbHPG - user248237
我不太确定你的意思,但无论如何,你都不能在顶部行上拥有三个面板和一个跨越整个底部行的面板。你需要使用两个图来实现这一点。 - baptiste

2

将图例从图形中分离出来(参见ggplot separate legend and plot),然后使用grid.arrange

library(gridExtra)
g_legend <- function(a.gplot){
      tmp <- ggplot_gtable(ggplot_build(a.gplot))
     leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
     legend <- tmp$grobs[[leg]]
     legend
 }
 legend1 <- g_legend(p1)
 legend2 <- g_legend(p2)

grid.arrange(p1 + theme(legend.position = 'none'), legend1, 
             p2 + theme(legend.position = 'none'), legend2,
            ncol=2, widths = c(5/6,1/6))

显然这是R的实现。


谢谢,但是如何将其翻译成rpy2并不清楚,我想避免定义新的纯R函数,因为这会使翻译变得更加复杂。 - user248237

1
使用gridExtragrid.arrange()进行答案的未经测试的翻译。图表的左侧(y轴标签所在位置)可能不总是对齐的。
from rpy2.robjects.packages import importr
gridextra = importr('gridExtra')
from rpy2.robjects.lib import ggplot2
_ggplot2 = ggplot2.ggplot2
def dollar(x, name): # should be included in rpy2.robjects, may be...
    return x[x.index(name)]

def g_legend(a_gplot):
    tmp = _ggplot2.ggplot_gtable(_ggplot2.ggplot_build(a_gplot))
    leg = [dollar(x, 'name')[0] for x in dollar(tmp, 'grobs')].index('guide-box')
    legend = dollar(tmp, 'grobs')[leg]
    return legend
legend1 = g_legend(p1)
legend2 = g_legend(p2)
nolegend = ggplot2.theme(**{'legend.position': 'none'})
gridexta.grid_arrange(p1 + nolegend, legend1, 
                      p2 + nolegend, legend2,
                      ncol=2, widths = FloatVector((5.0/6,1.0/6)))

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