boshek
boshek

Reputation: 4406

Formatting geom_label dates

Is there a way to format the dates printed by geom_label in ggplot2 beyond their raw value? In the example below the dates are printed as "1910-01-01". However, say I wanted to just print the month and day and also have the month as a text value. Anyone have any ideas how I'd go about doing that?

library(ggplot2)

df <- data.frame(
  x=1:5,
  y=runif(5,10,40),
  Date=seq(as.Date("1910/1/1"), as.Date("1914/1/1"), "years"))


ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_label(aes(label=Date))

Upvotes: 4

Views: 1901

Answers (1)

Se&#241;or O
Se&#241;or O

Reputation: 17412

I'm assuming you meant geom_text not geom_label.

You can use format to extract what you want (see format.POSIXct for full list)

 ggplot(df, aes(x=x, y=y)) +
     geom_point() +
     geom_text(aes(label=format(Date, format = "%b %d")))

enter image description here

Upvotes: 4

Related Questions