Question

I'm trying to change the background colors of strips on top of facet grids/facet wraps in ggplot2 and also remove their bounding boxes (i.e. just show the labels). It seems that this require element_rect which is not defined in rpy2 -- how can its constructor be defined? Or is there an alternative way of doing this without element_rect? I tried:

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

but it does not work, giving the error:

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

also,

from rpy2.robjects.lib.ggplot2 import element_rect

fails with an import error. how can this be done?

Was it helpful?

Solution

[edit: added unspecified named parameters passed to new() in answer to comments for the answer]

Pretty much the same answer as in how can theme_classic be accessed in rpy2 from ggplot2? , and since I am it I'll add the note that problems in rpy2 that one want to be eventually solved should be reported to the issue tracker.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top