Specification of first and last tick marks with scale_x_date

前端 未结 1 838
难免孤独
难免孤独 2020-11-30 12:36

I would like to be able to specify the first and last tick marks which appear in a plot produced by ggplot2, but an running into some problems. Here is some code.



        
相关标签:
1条回答
  • 2020-11-30 13:09

    To ensure that axis is not expanded you can add argument expand = c(0, 0) to scale_x_date().

    qplot(dateVec, myData) + 
    scale_x_date(breaks = "4 weeks", limits = c(min(dateVec), max = max(dateVec)),
                 expand=c(0,0)) +  
    theme(axis.text.x  = element_text(size = 10, angle = 45, colour = "black",
          vjust = 1, hjust = 1))
    

    enter image description here

    UPDATE

    If you need ticks that start with minimal and maximal dates then you can define your own breaks. For this I made vector break.vec containing minimal and maximal date as well as dates by month between them. Then used this vector to set breaks in scale_x_date().

    break.vec <- c(as.Date("2011-11-21"),
                   seq(from = as.Date("2011-12-01"), to = as.Date("2012-11-01"),
                     by = "month"),
                   as.Date("2012-11-23"))
    
    qplot(dateVec, myData) + 
      scale_x_date(breaks = break.vec) +  
      theme(axis.text.x = element_text(size = 10, angle = 45, colour = "black",
                                       vjust = 1, hjust = 1))
    

    enter image description here

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