I want to present a plot with a date variable formatted as year-mon (yearmon) in the x-axis.
I formatted the year-mon variable like this:
library("zoo")
GP$time <- as.yearmon(paste(GP$year, GP$month, sep = "-"))
head(GP$time) # look at it
plot(suiciderate ~ time, GP) # plot it
Giving this formatting:
[1] "Jan 2012" "Feb 2012" "Mar 2012" "Apr 2012" "May 2012"
I have problems specifying xmin and xmax for the following plot:
GP_treatment <- ggplot(GP,
aes(x = time, y = suiciderate, col = factor(intervention))) +
geom_smooth(se = FALSE) +
annotate("rect", xmin = "Jan 2016", xmax = "Jul 2016", ymin = -Inf, ymax = +Inf,
alpha = .8)
val = c("#E41A1C", "#377EB8")
lab = c("Control", "Treatment")
GP_treatment +
scale_x_continuous("Month", expand = c(0,0)) +
scale_y_continuous("Monthly suicide rate per 100 000", expand = c(0,0)) +
scale_color_manual("Treatment status",
values = val,
labels = lab)+
geom_point(alpha=.2)
Running this just returns:
Error: Discrete value supplied to continuous scale
I suspect xmin and xmax must be specified otherwise, but I haven't found the solution yet. Hopefully someone can help me out on this.