I am trying to use the rpy2 package to call ggplot2 from a python script to plot time series data. I get an error when I try to adjust the date limits of the x-scale. The rpy2 documentation provides this guidance (https://rpy2.readthedocs.io/en/version_2.8.x/vector.html?highlight=date%20vector): "Sequences of date or time points can be stored in POSIXlt
or POSIXct
objects. Both can be created from Python sequences of time.struct_time
objects or from R objects."
Here is my example code:
import numpy as np
import pandas as pd
import datetime as dt
from rpy2 import robjects as ro
from rpy2.robjects import pandas2ri
import rpy2.robjects.lib.ggplot2 as ggplot2
pandas2ri.activate()
#Create a random dataframe with time series data
df = pd.DataFrame({'Data': np.random.normal(50, 5, 10),
'Time': [dt.datetime(2000, 7, 23), dt.datetime(2001, 7, 15),
dt.datetime(2002, 7, 30), dt.datetime(2003, 8, 5),
dt.datetime(2004, 6, 28), dt.datetime(2005, 7, 23),
dt.datetime(2006, 7, 15), dt.datetime(2007, 7, 30),
dt.datetime(2008, 8, 5), dt.datetime(2009, 6, 28)]})
#Create a POSIXct vector from time.struct_time objects to store the x limits
date_min = dt.datetime(2000, 1, 1).timetuple()
date_max = dt.datetime(2010, 1, 1).timetuple()
date_range = ro.vectors.POSIXct((date_min, date_max))
#Generate the plot
gp = ggplot2.ggplot(df)
gp = (gp + ggplot2.aes_string(x='Time', y='Data') +
ggplot2.geom_point() +
ggplot2.scale_x_date(limits=date_range))
When I run this code, I get the following error message:
Error: Invalid input: date_trans works with objects of class Date only
Instead of the POSIXct
object, I have also tried the DateVector
object. I have also tried using base.as_Date()
to convert date strings into R dates and feeding those into the R vector objects. I always get the same error message. In R, I would change the scale limits like this:
gp + scale_x_date(limits = as.Date(c("2000/01/01", "2010/01/01"))
How do I translate this into rpy2 so that my python script will run?