R ggplot and facet grid: how to control x-axis breaks

后端 未结 1 503
既然无缘
既然无缘 2020-12-30 04:59

I am trying to plot the change in a time series for each calendar year using ggplot and I am having problems with the fine control of the x-axis. If I do not use scale

相关标签:
1条回答
  • 2020-12-30 05:35

    here is an example:

    df <- transform(df, doy = as.Date(paste(2000, month, day, sep="/")))
    
    p <- ggplot(df, aes(doy, pctchg)) +
     geom_line( aes(group = 1, colour = pctchg),size=0.75) + 
     facet_wrap( ~ year, ncol = 2) +
     scale_x_date(format = "%b") +
     scale_y_continuous(formatter = "percent") +
     opts(legend.position = "none")
    p
    

    enter image description here

    Do you want this one?

    The trick is to generate day of year of a same dummy year.

    UPDATED

    here is an example for the dev version (i.e., ggplot2 0.9)

    p <- ggplot(df, aes(doy, pctchg)) +
      geom_line( aes(group = 1, colour = pctchg), size=0.75) + 
      facet_wrap( ~ year, ncol = 2) +
      scale_x_date(label = date_format("%b"), breaks = seq(min(df$doy), max(df$doy), "month")) +
      scale_y_continuous(label = percent_format()) +
      opts(legend.position = "none")
    p
    

    enter image description here

    0 讨论(0)
提交回复
热议问题