0

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?

1 Answers1

1

[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
Community
  • 1
  • 1
lgautier
  • 11,363
  • 29
  • 42
  • If I do that it fails to accept the `fill=` argument: `'strip.background': ggplot2.element_rect(fill="white")}) -> TypeError: new() got an unexpected keyword argument 'fill'` –  Sep 01 '13 at 18:39
  • same behaviour occurs with `colour` or these arguments http://docs.ggplot2.org/0.9.2.1/element_rect.html –  Sep 01 '13 at 18:56
  • 1
    Just add the arguments to the signature of the classmethod `new', as it is done for ElementText (https://bitbucket.org/lgautier/rpy2/src/ed79a71b7d8da5fa9857365890b9cdc572e183b1/rpy/robjects/lib/ggplot2.py?at=default#cl-661), and submit a patch when done. – lgautier Sep 01 '13 at 19:13
  • The fix is in the repository, and will be included in rpy2 >= 2.3.7 . – lgautier Sep 01 '13 at 20:17