English 中文(简体)
ggplot2 hell with rpy2-2.0.7 + python 2.6 + r 2.11 (windows 7)
原标题:

i am using rpy2-2.0.7 (i need this to work with windows 7, and compiling the binaries for the newer rpy2 versions is a mess) to push a two-column dataframe into r, create a few layers in ggplot2, and output the image into a <.png>.

i have wasted countless hours fidgeting around with the syntax; i did manage to output the files i needed at one point, but (stupidly) did not notice and continued fidgeting around with my code ...

i would sincerely appreciate any help; below is a (trivial) example for demonstration. Thank you very much for your help!!! ~ Eric Butter


import rpy2.robjects as rob
from rpy2.robjects import r
import rpy2.rlike.container as rlc
from array import array

r.library("grDevices")    # import r graphics package with rpy2
r.library("lattice")
r.library("ggplot2")
r.library("reshape")

picpath =  foo.png  

d1 = ["cat","dog","mouse"]
d2 = array( f ,[1.0,2.0,3.0])

nums = rob.RVector(d2)
name = rob.StrVector(d1)

tl = rlc.TaggedList([nums, name], tags = ( nums ,  name ))
dataf = rob.RDataFrame(tl)

## r[ png ](file=picpath, width=300, height=300)
## r[ ggplot ](data=dataf)+r[ aes_string ](x= nums )+r[ geom_bar ](fill= name )+r[ stat_bin ](binwidth=0.1)
r[ ggplot ](data=dataf)
r[ aes_string ](x= nums )
r[ geom_bar ](fill= name )
r[ stat_bin ](binwidth=0.1)
r[ ggsave ]()
## r[ dev.off ]()

*The output is just a blank image (181 b).


here are a couple common errors R itself throws as I fiddle around in ggplot2:

r[ png ](file=picpath, width=300, height=300)
r[ ggplot ]()
r[ layer ](dataf, x=nums, fill=name, geom="bar")
r[ geom_histogram ]()
r[ stat_bin ](binwidth=0.1)
r[ ggsave ](file=picpath)
r[ dev.off ]()

*RRuntimeError: Error: No layers in plot

r[ png ](file=picpath, width=300, height=300)
r[ ggplot ](data=dataf)
r[ aes ](geom="bar")
r[ geom_bar ](x=nums, fill=name)
r[ stat_bin ](binwidth=0.1)
r[ ggsave ](file=picpath)
r[ dev.off ]()

*RRuntimeError: Error: When setting aesthetics, they may only take one value. Problems: fill,x

最佳回答

I use rpy2 solely through Nathaniel Smith s brilliant little module called rnumpy (see the "API" link at the rnumpy home page). With this you can do:

from rnumpy import *

r.library("ggplot2")

picpath =  foo.png  
name = ["cat","dog","mouse"]
nums = [1.0,2.0,3.0]

r["dataf"] = r.data_frame(name=name, nums=nums)
r("p <- ggplot(dataf, aes(name, nums, fill=name)) + geom_bar(stat= identity )")
r.ggsave(picpath)

(I m guessing a little about how you want the plot to look, but you get the idea.)

Another great convenience is entering "R mode" from Python with the ipy_rnumpy module. (See the "IPython integration" link at the rnumpy home page).

For complicated stuff, I usually prototype in R until I have the plotting commands worked out. Error reporting in rpy2 or rnumpy can get quite messy.

For instance, the result of an assignment (or other computation) is sometimes printed even when it should be invisible. This is annoying e.g. when assigning to large data frames. A quick workaround is to end the offending line with a trailing statement that evaluates to something short. For instance:

In [59] R> long <- 1:20
Out[59] R>
  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
 [19]  19  20

In [60] R> long <- 1:100; 0
Out[60] R> [1] 0

(To silence some recurrent warnings in rnumpy, I ve edited rnumpy.py to add from warnings import warn and replace print "error in process_revents: ignored" with warn("error in process_revents: ignored") . That way, I only see the warning once per session.)

问题回答

You have to engage the dev() before you shut it off, which means that you have to print() (like JD guesses above) prior to throwing dev.off().

from rpy2 import robjects                          
r = robjects.r                                                                                    
r.library("ggplot2")
robjects.r( p = ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar() ) 
r.ggsave( /stackBar.jpeg ) 
robjects.r( print(p) )
r[ dev.off ]()

To make it slightly more easy when you have to draw more complex plots:

from rpy2 import robjects
from rpy2.robjects.packages import importr
import rpy2.robjects.lib.ggplot2 as ggplot2
r = robjects.r
grdevices = importr( grDevices )
p = r(   
  library(ggplot2)

  p <- ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()
  p <- p + opts(title = "{0}")
    # add more R code if necessary e.g. p <- p + layer(..)
  p   .format("stackbar")) 
  # you can use format to transfer variables into R
  # use var.r_repr() in case it involves a robject like a vector or data.frame
p.plot()
# grdevices.dev_off()




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签