Plot separate years on a common day-month scale

后端 未结 3 1881
迷失自我
迷失自我 2020-12-20 08:26

I want to create a time series plot of temperatures for the summers of 2012 and 2013.

The only problem is that I want the data series to plot one on top of the oth

3条回答
  •  醉话见心
    2020-12-20 08:55

    If your base dataset is temp and date, then this avoids manipulating the original data frame:

    ggplot(df) +
      geom_point(aes(x=strftime(date,format="%m-%d"),
                     y=temp, 
                     color=strftime(date,format="%Y")), size=3)+
      scale_color_discrete(name="Year")+
      labs(x="date")
    

    EDIT (Response to OP's comment).

    So this combines the approach above with Henrik's, using dates instead of char for the x-axis, and avoiding modification of the original df.

    library(ggplot2)
    ggplot(df) +
      geom_point(aes(x=as.Date(paste(2014,strftime(date,format="%m-%d"),sep="-")),
                     y=temp, 
                     color=strftime(date,format="%Y")), size=3)+
      scale_color_discrete(name="Year")+
      labs(x="date")
    

提交回复
热议问题