【问题标题】:customizing strip of grids in ggplot2 in rpy2在 rpy2 中自定义 ggplot2 中的网格条
【发布时间】:2013-09-04 20:15:53
【问题描述】:

我正在尝试更改 ggplot2 中构面网格/构面环绕顶部的条带的背景颜色,并删除它们的边界框(即仅显示标签)。这似乎需要element_rect,它没有在 rpy2 中定义——如何定义它的构造函数?或者有没有element_rect 的替代方法?我试过了:

my_plot += theme(**{'strip.background': element_blank()})

但它不起作用,给出错误:

rpy2.rinterface.RRuntimeError: Error in (function (el, elname)  : 
  Element panel.background must be a element_rect object.

还有,

from rpy2.robjects.lib.ggplot2 import element_rect

因导入错误而失败。如何才能做到这一点?

【问题讨论】:

    标签: python ggplot2 rpy2


    【解决方案1】:

    [编辑:添加未指定的命名参数传递给 new() 以回答 cmets 的答案]

    答案与 how can theme_classic be accessed in rpy2 from ggplot2? ,既然我是它,我将添加注释,即希望最终解决的 rpy2 中的问题应报告给问题跟踪器。

    import rpy2.robjects.lib.ggplot2 as ggplot2
    
    class ElementRect(ggplot2.Element):
        _constructor = ggplot2.ggplot2.element_rect
        @classmethod
        def new(cls, **kwargs):
            res = cls(cls._constructor(**kwargs))
            return res
    
    # Monkey patch ggplot2
    ggplot2.element_rect = ElementRect.new
    

    【讨论】:

    • 如果我这样做,它将无法接受fill= 参数:'strip.background': ggplot2.element_rect(fill="white")}) -> TypeError: new() got an unexpected keyword argument 'fill'
    • colour 或这些参数 docs.ggplot2.org/0.9.2.1/element_rect.html 会发生相同的行为
    • 只需将参数添加到类方法“new”的签名中,就像对 ElementText (bitbucket.org/lgautier/rpy2/src/…) 所做的那样,完成后提交补丁。
    • 修复在存储库中,将包含在 rpy2 >= 2.3.7 中。
    猜你喜欢
    • 2013-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多