1

I am trying to plot cumulative sums of groups over time, and add corresponding linear prediction lines for each group. The plot turns out well, however, I cannot read the dates of the prediction slopes on the x-axis as these are numbers.

How can I change the code below so that stat_smooth returns dates instead of numbers?

g <- ggplot(aes(x = created_at_day, y = sum, color = groups), data= df) +
  geom_line() +
  stat_smooth(aes(x = as.Date(created_at_day), y = sum, color = groups), method = "lm", fullrange = TRUE, se = FALSE, size = 0.1) + 
  xlab('Date') + 
  ylab('cumulative sum of patients') + 
  expand_limits(x = as.Date(c("2017-11-13", "2018-04-01"))) +
  ggtitle('Number of patients included per practice') +
  theme_bw()
g
ggplotly(g)

EDIT: Here is some code to generate the plot above.

set.seed(101)
created_at_day= sample(seq(as.Date('2017-01-01'), as.Date('2018-01-01'), by = "day"), 300)

set.seed(101)
groups= sample(1:3,300, replace= TRUE)
df = data.frame(created_at_day, groups)

group1= seq(1, table(groups)[[1]]*1.5, 1.5)
group2= seq(0.8, table(groups)[[2]]*0.8, 0.8)
group3= seq(1, table(groups)[[3]]*1.8, 1.8)

df_g1= subset(df, groups == 1)
df_g1 = arrange(df_g1, created_at_day)
df_g1= cbind(df_g1, sum= group1)
df_g2= subset(df, groups == 2)
df_g2 = arrange(df_g2, created_at_day)
df_g2= cbind(df_g2, sum= group2)
df_g3= subset(df, groups == 3)
df_g3 = arrange(df_g3, created_at_day)
df_g3= cbind(df_g3, sum= group3)
df= rbind(df_g1, df_g2, df_g3)
df$groups= as.factor(df$groups)
Andrew Lavers
  • 4,328
  • 1
  • 12
  • 19
mizzlosis
  • 515
  • 1
  • 4
  • 17
  • Welcome @jkhuc Without sample data it is hard to help. I think you should use `ggplot(aes(x=as.Date(created_at_day)` . then you dont need the x aesthetic in the `stat_smooth`. You may also need scale_x_date – Andrew Lavers Mar 18 '18 at 23:51
  • Thanks! I edited the post and added some code to generate data. – mizzlosis Mar 19 '18 at 01:05
  • This appears to be an issue with ggplot. See these articles. https://stackoverflow.com/questions/44770799/date-format-in-tooltip-of-ggplotly https://community.plot.ly/t/date-format-in-tooltip-of-ggplotly/4766/2 – Andrew Lavers Mar 19 '18 at 08:52
  • Good to know, thanks. – mizzlosis Mar 19 '18 at 11:12

0 Answers0