0

I'm having trouble getting my plot to display dates (ie. 23/01) instead of weekday names (ie. Thu). My dataset consists of dates and measurements of bat activity. I've set the 'Dates' column of my data as as.Date in the format "%d.%m.%y" and whenever I plot my graph I get weekday names instead of dates.

My code looks like this:

rdate<-as.Date(df,"%d.%m.%Y")
plot(df$Afromontane)

My plot ends up looking like this (below). It's all fine except I'd like the weekday names to be dates in the format (d/m).

enter image description here

df looks like this:

structure(list(Date = c("23.01.20", "24.01.20", "25.01.20", "26.01.20", 
"27.01.20", "28.01.20", "29.01.20"), Afromontane = c(13.67, 0, 
0, 1.67, 3.67, 22, 3.33), Milkwood = c(8.33, 3.67, 8, 8.33, 4.33, 
6.33, 1)), row.names = c(NA, -7L), class = c("tbl_df", "tbl", 
"data.frame"))
steveb
  • 5,382
  • 2
  • 27
  • 36
  • Hi there, could you please provide a minimal example? Would be easier to help you if we have an idea of how `df` looks... – Coy Mar 17 '20 at 17:59
  • https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – mccurcio Mar 17 '20 at 18:04
  • @Coy I've added a picture of my df – Lindes De Waal Mar 17 '20 at 18:06
  • @LindesDeWaal Please put the output of `dput(df)` in the post instead of an image. – steveb Mar 17 '20 at 18:11
  • You are missing part of the output of `dput`. Also, please don't include things like `Thanks in advance` in the question. We try to keep the question concise and clear of un needed items. – steveb Mar 17 '20 at 18:17
  • I can't reproduce your issue. Please follow @oaxacamatt's link and provide a minimal data set and code... – Coy Mar 17 '20 at 18:22
  • @LindesDeWaal I have updated your post twice to include the plot in the post not as a link, can you please stop replacing with the link. – steveb Mar 17 '20 at 19:24

1 Answers1

0

A minimal example using ggplot2:

library(ggplot2)

df = data.frame(date = sample(seq(as.Date('2001/01/01'), as.Date('2003/01/01'), by="day"), 10), x = runif(10, 1, 10))

df$shortdate <- format(df$date, format="%m-%d")

ggplot(df, aes(x = shortdate, y = x)) +
      geom_point()

enter image description here

Alternatively, using base R:

df = data.frame(date = sample(seq(as.Date('2001/01/01'), as.Date('2003/01/01'), by="day"), 10), x = runif(10, 1, 10))

plot(as.Date(df$date), df$x,xaxt = "n", type = "p")
axis(1, df$date, format(df$date, "%m-%d"))

enter image description here

Joris
  • 417
  • 4
  • 17
  • Given the OP used `plot` (base R), you may want to also provide the base R equivalent answer. – steveb Mar 17 '20 at 18:19