set date range in ggplot

后端 未结 3 1998
迷失自我
迷失自我 2020-12-14 03:03

my data frame is z:

library(ggplot2); library(scales)
z <-     structure(list(Month = structure(c(14975, 15095, 15156, 15187, 
15248), class = "Date&q         


        
3条回答
  •  伪装坚强ぢ
    2020-12-14 03:26

    Here's a solution using ggplot 3.1 which requires the least tweaks to the original code:

    ggplot(z, aes(Month, Value)) + 
        geom_bar(fill="orange",size=.3, stat="identity", position="identity") +
        geom_smooth(data=z,aes(Month,Value,group=1), method="lm", size=2, color="navyblue") + 
        scale_x_date(date_breaks = "1 month", 
               limits = as.Date(c('1/1/2011', '1/1/2013'), format="%d/%m/%Y"),
               date_labels="%b-%Y" ) +
        theme(axis.text.x = element_text(angle = 90))
    

    the theme() at the end is optional, but makes the formatting easier to read if you want to use your original "%b-%Y" formatting string.

提交回复
热议问题