Reputation:
Does rpy2 have an equivalent of scale_size_area()
from ggplot2? If not, how can I write it?
I'm referring to this function:
http://docs.ggplot2.org/0.9.3/scale_size_area.html
I would like to scale points by size according to area, not radius which I believe is the default.
Upvotes: 2
Views: 337
Reputation: 411
No, rpy2 does not have this feature since the ggplot2 wrapper module was actually hand coded. (Hopefully, as the author notes metaprogramming will ultimately be used to make the code shorter and more automatic.) However, the good news is that it is not hard to add your own feature. Here's how you add scale_size_area() to the existing functionality:
import rpy2.robjects.lib.ggplot2 as ggplot2
import rpy2.robjects as ro
mtcars = ro.r('mtcars')
gp = ggplot2.ggplot(mtcars)
pp = gp + ggplot2.aes_string(x='wt', y='mpg', size='cyl') + ggplot2.geom_point()
# using the standard size (radius) aesthetic for size
pp.plot()
# create the ScaleSizeArea (based on ggplot2.py in rpy2)
class ScaleSizeArea(ggplot2.ScaleSize):
_constructor = ggplot2.ggplot2_env['scale_size_area']
ggplot2.scale_size_area = ScaleSizeArea.new
# now you can use scale_size_area instead!
pp_area = pp + ggplot2.scale_size_area()
pp_area.plot()
Upvotes: 2