If you have the R code, porting to rpy2 can be taken progressively
import rpy2.robjects as ro
ro.globalenv['dr'] = dr
ro.r("""
library(ggplot2)
names(dr) <- c('letters','bool','n','value')
x <- ggplot() +
geom_bar(data=dr, aes(y = value, x = letters, fill = bool),
stat="identity", position='stack') +
theme_bw() +
facet_grid( ~ n)
print(x)
""")
The drawback with this is that R's GlobalEnv is used. A function can be more elegant.
make_plot = ro.r("""
function(dr) {
names(dr) <- c('letters','bool','n','value')
x <- ggplot() +
geom_bar(data=dr, aes(y = value, x = letters, fill = bool),
stat="identity", position='stack') +
theme_bw() +
facet_grid( ~ n)
print(x)
}""")
make_plot(dr)
An alternative is to use the ggplot2 mapping in rpy2, and write this without writting R code:
from rpy2.robjects import Formula
from rpy2.robjects.lib.ggplot2 import ggplot, geom_bar, aes_string, theme_bw, facet_grid
## oddity with names in the examples, that can either be corrected in the Python-pandas
## structure or with an explicit conversion into an R object and renaming there
drr = rpy2.robjects.pandas2ri.pandas2ri(dr)
drr.names[2] = 'n'
drr.names[3] = 'value'
p = ggplot(drr) + \
geom_bar(aes_string(x="letters", y="value", fill="bool"),
stat="identity", position="stack") + \
theme_bw() + \
facet_grid(Formula('~ n'))
p.plot()