Breaks for scale_x_date in ggplot2 and R

前端 未结 1 505
余生分开走
余生分开走 2020-12-10 14:19

I am plotting value~date in ggplot2 (in R). I have the following code. As you see ggplot2 adds more breaks on the x-axis that I have in my data. I just want to have the x-la

相关标签:
1条回答
  • 2020-12-10 14:48

    One approach would be to treat the x-axis as numeric and set the breaks and labels aesthetics with scale_x_continuous().

    ggplot(my.data, aes(as.numeric(date), vals)) + 
      geom_line(size = 1.5) +
      scale_x_continuous(breaks = as.numeric(my.data$date)
                         , labels = format(my.data$date, format = "%m/%d"))
    

    though the break between 7/24 through 7/28 looks a bit strange in my opinion. However, I think that's what you want? Let me know if I've misinterpreted.

    EDIT

    As noted above, I wasn't thrilled with the way the breaks looked above, specifically with the gray grid in the background. Here's one way to maintain the rectangular grid and to only label the points where we have data. You could do this all within the ggplot call, but I think it's easier to do the processing outside of ggplot. First, create a vector that contains the sequence of numbers corresponding to the dates. Then we'll update the appropriate labels and replace the NA entries with " " to prevent anything from being plotted on the x-axis for those entries:

    xscale <- data.frame(breaks = seq(min(as.numeric(my.data$date)), max(as.numeric(my.data$date)))
                          , labels = NA)
    
    xscale$labels[xscale$breaks %in% as.numeric(my.data$date)] <- format(my.data$date, format = "%m/%d") 
    xscale$labels[is.na(xscale$labels)] <- " "
    

    This gives us something that looks like:

      breaks labels
    1  15177  07/22
    2  15178  07/23
    3  15179  07/24
    4  15180       
    5  15181       
    6  15182       
    7  15183  07/28
    8  15184  07/29
    

    which can then be passed to the scale like this:

    scale_x_continuous(breaks = xscale$breaks, labels = xscale$labels)

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