R: ggplot display all dates on x axis

22,687

Solution 1

The easiest way would be to use scale_x_date

ggplot(data = df) + 
  geom_point(mapping = aes(x = Date, y = WearRate, color = Wheel))+
  scale_x_date(date_labels="%b %y",date_breaks  ="1 month")

%b: Abbreviated month name
%y: Year without century
For a description of the full possibilities see ?strftime()

Solution 2

ggplot(data = df) + 
  geom_point(mapping = aes(x = Date, y = WearRate, color = Wheel))+
  scale_x_date(date_labels="%b %Y", breaks = unique(df$Date))

enter image description here

Share:
22,687

Related videos on Youtube

chintan s
Author by

chintan s

Updated on August 29, 2021

Comments

  • chintan s
    chintan s over 2 years

    I have the following data set

    structure(list(Date = structure(c(16636, 16667, 16698, 16728, 
    16759, 16789, 16820, 16851, 16880, 16911, 16636, 16667, 16698, 
    16728, 16759, 16789, 16820, 16851, 16880, 16911, 16636, 16667, 
    16698, 16728, 16759, 16789, 16820, 16851, 16880, 16911, 16636, 
    16667, 16698, 16728, 16759, 16789, 16820, 16851, 16880, 16911, 
    16636, 16667, 16698, 16728, 16759, 16789, 16820, 16851, 16880, 
    16911), class = "Date"), Wheel = structure(c(5L, 5L, 5L, 5L, 
    5L, 5L, 5L, 5L, 5L, 5L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
    12L, 12L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 11L, 11L, 11L, 
    11L, 11L, 11L, 11L, 11L, 11L, 11L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L), .Label = c("L1", "L2", "L3", "L4", "L5", "L6", "R1", 
    "R2", "R3", "R4", "R5", "R6"), class = "factor"), WearRate = c(-0.000367, 
    0, 0, 0, 0.001888, 0, -0.00018, 0.000579, -0.000211, 0.000643, 
    0.000106, 0, 0, 0, 0.000833, 0, -0.00036, 0.000811, -0.000819, 
    0.002044, -0.00029, 0, 0, 0, 0.001666, 0, -0.000348, 0.000888, 
    -0.000679, 0.001636, 8.7e-05, 0, 0, 0, 0.000666, 0, -0.000315, 
    0.000618, -0.000585, 0.001636, -0.000512, 0, 0, 0, 0.002499, 
    0, -0.000247, 0.000734, -9.4e-05, 0.000409)), .Names = c("Date", 
    "Wheel", "WearRate"), row.names = 211269:211318, class = "data.frame")
    

    I am trying to make a plot of Date vs WearRate and color by Wheel. The code is as follows:

    ggplot(data = df) + geom_point(mapping = aes(x = Date, y = WearRate, color = Wheel))
    

    It works but I want to put actual date labels. How do I do it?

    Edit

    The plot currently looks as shown here. However, I want to see "Aug 2015", "Sep 2015" etc on X axis and I want to display all the ticks.

    enter image description here

  • chintan s
    chintan s over 7 years
    This works but it only shows 3 ticks. I want to display all the ticks.
  • Haboryme
    Haboryme over 7 years
    I now shows each month. Lmk if that's how it should be.
  • Layheang Song
    Layheang Song over 2 years
    I've got the same problem as yours. You may have got the solution, but here I want to share it with the others who are searching for it. It works for my case. I hope it will works for you, too.
  • StupidWolf
    StupidWolf over 2 years
    I think you can also explain why the unique() works actually and how it's different from the accepted answer
  • Layheang Song
    Layheang Song over 2 years
    Thank you for your suggestion. unique(0) is for showing only the date where the data exists.