0

I visualized my data with a ggplot2-code. It worked really well, my only issue is the x-axis. R named all the dates, which lead to a chaotic state of the x-axis.

enter image description here

ggplot(Oct_Apr)+   aes(x=date, y=ctr, group=1) +   geom_line(linewidth = 0.7) +   geom_smooth(method = "gam", se = TRUE, alpha = 0.5) + scale_y_continuous(breaks = c(10000,20000,50000,100000,200000,300000,400000,500000),labels=comma) +   geom_hline(mapping = aes(yintercept = min(Oct_Apr$ctr)), linetype = "dashed", color = "red") +   geom_hline(mapping = aes(yintercept = max(Oct_Apr$ctr)), linetype = "dashed", color = "red") +   theme_linedraw() +   labs(x="date“, y="(CTR)", title = „Click-Through-Rate Oct. 2019 - Apr. 2020",  subtitle = "Click-Through-Rate (CTR) - Min. 27.02.2020; Max. 15.11.2019")

Maybe someone has an idea of how I can improve the x-axis of this ggplot2 graph?

I have combined the time values ​​in a date variable so that I can visualize the entire time period. However, the consequence is that the date_breaks command doesn't seem to work, as well as scale_x_date(breaks=waiver("2019.10.01","2019.11.01", "2019.12.01", "2020.01.01", "2020.02.01", "2020.03.01", "2020.04.01“).

  • "A waiver is a 'flag' object ... that indicates the calling function should just use the default value". Using `waiver` you are requesting to use default breaks. Define the breaks with `c()`instead, or `seq (as.Date('2019-10-01'), as.Date('2020-04-01'), by = 'month')`. – M Aurélio Jan 31 '23 at 13:26
  • Do you mean like this: scale_x_date(breaks=c("2019.10.01","2019.11.01", "2019.12.01", "2020.01.01", "2020.02.01", "2020.03.01", "2020.04.01")) or date_breaks(seq(as.Date('2019.10.01'), as.Date('2020.04.01'), by = 'month'))? – Laura Wagner Jan 31 '23 at 13:46
  • 2
    Hi Laura. Are you _sure_ your dates are actually dates rather than character strings? It looks like the kind of plot you get when you forget to turn dates stored in character format to actual dates. Perhaps try `aes(x=lubridate::ymd(date), y=ctr)`. Date format in R is not the same as character strings that look like dates. If the numbers are separated by periods rather than dashes, then your data is certainly not in an actual date format. – Allan Cameron Jan 31 '23 at 13:56
  • @LauraWagner in `breaks` you define the breaks explicitly from a vector or a function. `date_breaks` you need to say the incremet, like `date_breaks = '1 month'` – M Aurélio Jan 31 '23 at 14:47
  • In R, "2019.10.01" is a character string like "orange" and will be treated like a category in ggplot2, and every single value will be shown in the x axis. If you run `as.Date("2019.10.01", format = "%Y.%m.%d")` or `lubridate::ymd("2019.10.01")` R will convert that character string to a `Date` data type, which it understands has a linear mapping to time. When ggplot maps `Date` data, it will space it correctly and provide reasonably axis breaks and labels. – Jon Spring Jan 31 '23 at 15:29
  • Thank you for the explanation, Allen! And with aes(x=lubridate::ymd(date), y=ctr) I got a really nice sorted x-axis! @AllanCameron – Laura Wagner Feb 01 '23 at 20:56
  • Thank you, Shawn! You are right, it's definitely helpful if you can directly try out your idea on the data and it's amazing that there is an option to share at least a part of the data here! @ShawnHemelstrand – Laura Wagner Feb 01 '23 at 20:56
  • Thank you, Jon! That's a really good explanation! @JonSpring – Laura Wagner Feb 01 '23 at 21:02

0 Answers0