Skip to content

Instantly share code, notes, and snippets.

@etheleon
Last active June 19, 2018 03:49
Show Gist options
  • Save etheleon/259b91c16a083501a9330edf505f851b to your computer and use it in GitHub Desktop.
Save etheleon/259b91c16a083501a9330edf505f851b to your computer and use it in GitHub Desktop.
using ggplot2 in python
from rpy2.robjects.packages import importr
import rpy2.robjects.lib.ggplot2 as ggplot2 # don't use importr cause the ggplot2 here has the .plot() function
grdevices = importr('grDevices')
cowplot = importr('cowplot') #cause i love cowplot
rfeather = importr("feather", on_conflict="warn")
# Out[57]:
# country variable value
# 0 H random 15.872105
# 1 J random 24.370636
# 2 M random 1.015055
# 3 P random 11.118215
# 4 S random 0.885525
# 5 T random 0.882241
# 6 T random 1.093211
# 7 H svd 11.544371
# 8 J svd 0.647587
# 9 M svd 0.706966
# 10 P svd 7.971626
# 11 S svd 0.569839
# 12 T svd 0.588953
# 13 T svd 0.802850
write_dataframe(rmseDF, "temp.feather")
rmseDF_r = rfeather.read_feather("temp.feather")
grdevices.pdf(file="histo.pdf", width=10, height=10)
pp = ggplot2.ggplot(rmseDF_r) + \
ggplot2.aes_string(x='variable', y='value', fill='variable') + \
ggplot2.geom_histogram(stat="identity") + \
ggplot2.facet_wrap("country", scale="free") + \
ggplot2.theme_cowplot()
pp.plot()
grdevices.dev_off()
@etheleon
Copy link
Author

etheleon commented Jun 19, 2018

pandas2ri.py2ri function converts the types weirdly, recommended to use feather.
dun using importr("ggplot2") the the pp.plot() function is in the ggplot2 object. (but its a wrapper written by the rpy2 guys)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment