I try to get the last graph shown on the site, with its functions:
def custom_date_format3(breaks):
"""
Function to format the date
"""
res = []
for x in breaks:
# First day of the year
if x.month == 1:
fmt = '%Y'
else:
fmt = '%b'
res.append(date.strftime(x, fmt))
return res
def custom_date_breaks(width=None):
"""
Create a function that calculates date breaks
It delegates the work to `date_breaks`
"""
def filter_func(limits):
breaks = date_breaks(width)(limits)
# filter
return [x for x in breaks if x.month % 2]
return filter_func
(ggplot(economics.loc[40:60, :])
+ geom_point(aes('date', 'psavert'))
+ scale_x_datetime( # modified
breaks=custom_date_breaks('1 months'),
labels=custom_date_format3)
+ labs(y='personal saving rate')
)
and did not achieve the expected graph.
and did not achieve the expected graph.
These are the functions i use
How to obtain the graph without the need of these functions? Only using the functions of the ggplot2 package:
scale_date {ggplot2}
Position scales for date / time data
DT[,custom_date_breaks(date)][1:30]
[1] NA NA "1967-08-01" NA "1967-10-01" NA "1967-12-01" NA "1968-02-01"
[10] NA "1968-04-01" NA "1968-06-01" NA "1968-08-01" NA "1968-10-01" NA
[19] "1968-12-01" NA "1969-02-01" NA "1969-04-01" NA "1969-06-01" NA "1969-08-01"
[28] NA "1969-10-01" NA
I try to get the graph displayed,as above posted
I have performed the functions of the site to R. I have tried it, my knowledge of python is that of having used other software.
So I also asked: if it is not possible to obtain, from the data, the same graph using the proper functions of ggplot2.?
Thanks, @Hermes, that is helpful. I didn't realize you were trying to write the code in python to describe what you intended -- I had thought you were posting actual python code from a package you had mentioned earlier. I might suggest you try pseudocode in R, instead, referring to the documentation specifications you're implementing, in much the way it looks like you tried to develop your python code. That would be helpful, since folks here may not be familiar with python syntax, and may have trouble following you.
I'll come back again when I can, but if you look at the R documentation for scale_x_date() (which I'm sure you did, from your python code), it says breaks should be one of
NULL for no breaks
waiver() for the breaks specified by date_breaks
A Date/POSIXct vector giving positions of breaks
A function that takes the limits as input and returns breaks as output
so the last two would apply in your case, and it says limits should be one of:
NULL to use the default scale range
A numeric vector of length two providing limits of the scale. Use NA to refer to the existing minimum or maximum
A function that accepts the existing (automatic) limits and returns new limits
where, again, the last two apply in your case.
From this, it looks like limits are stored in any case as a vectors of length two, indicating the min and max, so the function version of breaks should probably takes these as input and return the third type of breaks, namely a "Date/POSIXct vector giving positions of breaks".
I hope this helps, and thanks again for the clarification.
with a change in function:
custom_date_format2
AND THESE LIMITS
limits = as.Date(c("1970-11-01","1972-09-01")
is the closest I have come to the expected graph.
If I vary the limits, it will disappear: the labels with years or the labels with names of months.