ggplot2 scale x date?

后端 未结 2 1712
孤街浪徒
孤街浪徒 2021-01-11 21:54

I would like to make the ticks in the following graph daily instead of every 2 days.

df1 <- structure(
  list(
    Timestamp = structure(
      c(
                


        
相关标签:
2条回答
  • 2021-01-11 22:34

    You can use the scales package. It looks like you need to use the function date_breaks, rather than just breaks to get proper labels:

    EDIT: it appears the argument has been changed to date_breaks:

    library(scales)
    library(ggplot2)
    ggplot(df1, aes(x = Timestamp, y = number)) +
        geom_line(size=2) +
        geom_point(size=5) + 
        scale_y_continuous(breaks = seq(0, 50, by = 2)) + 
        scale_x_datetime(date_breaks = "1 day")
    

    original:

    library(scales)
    library(ggplot2)
    ggplot(df1, aes(x = Timestamp, y = number)) +
        geom_line(size=2) +
        geom_point(size=5) + 
        scale_y_continuous(breaks = seq(0, 50, by = 2)) + 
        scale_x_datetime(breaks = date_breaks("1 day"))
    

    If you want to change how the date is displayed in the label, you can use date_format inside the scale_x_datetime call.

    0 讨论(0)
  • 2021-01-11 22:35

    Instead of using scale_x_datetime(breaks = "1 day"), try for scale_x_datetime(date_breaks = "1 day")

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