Error when customizing dates (years) on the x-axis in R using scales_x_date

I am trying to customise the x-axis (date) into 5 year intervals in R and it keeps returning the following error:

Error in scale_x_date(breaks = ("5 years"), labels = date_format("%Y")) : could not find function "scale_x_date"

Yes I ran the ggplot2 function. I want to set 5 year intervals in my date axis.

brakes takes a vector of specific points not a character string , I think you want to use date_breaks instead. See the documentation.

date_breaks A string giving the distance between breaks like "2 weeks", or "10 years". If both breaks and date_breaks are specified, date_breaks wins.

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Below is my reprex. Kindly assist.

structure(list(Date = structure(c(-1341, -1310, -1280, -1249, 
-1218, -1188, -1157, -1127, -1096, -1065, -1037, -1006, -976, 
-945, -915, -884, -853, -823, -792, -762, -731, -700, -671, -640, 
-610, -579, -549, -518, -487, -457, -426, -396, -365, -334, -306, 
-275, -245, -214, -184, -153, -122, -92, -61, -31, 0, 31, 59, 
90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 396, 424, 455, 
485, 516, 546, 577, 608, 638, 669, 699, 730, 761, 790, 821, 851, 
882, 912, 943, 974, 1004, 1035, 1065, 1096, 1127, 1155, 1186, 
1216, 1247, 1277, 1308, 1339, 1369, 1400, 1430, 1461, 1492, 1520, 
1551, 1581, 1612, 1642, 1673), class = "Date"), Sum.of.RainfallMbazwanaA = c(11.938, 
45.72, 6.35, 14.55166, 36.322, 31.496, 87.884, 143.51, NA, NA, 
NA, NA, 106.934, NA, NA, NA, 99.85756, NA, NA, NA, 30.48, 182.372, 
32.258, 36.322, 24.13, 12.446, 6.477, 30.226, NA, 29.464, 113.284, 
21.082, 40.894, 38.354, NA, NA, 30.988, 8.89, 56.134, 1.524, 
44.958, 225.806, 72.39, 85.09, 12.7, 42.926, 38.354, 25.4, 55.626, 
32.004, 3.302, 17.526, 22.098, 4.89, 94.234, 40.64, 80.518, 166.878, 
419.608, 127.254, 28.702, 13.208, 36.068, 2.032, 35.052, 170.942, 
NA, 221.742, 154.178, 154.178, NA, 27.178, 162.814, 13.462, 29.464, 
3.048, 10.414, 36.83, 72.39, 104.902, 56.642, 174.752, 19.05, 
116.586, 33.528, 37.846, 30.48, 27.686, 399.796, 55.626, 190.5, 
145.288, 187.452, 93.472, 95.758, 114.3, 70.358, 15.494, 158.75, 
4.318)), row.names = c(NA, 100L), class = "data.frame")

And the code for the graph?

plot(SAWS_Monthly,xlab="Date",ylab="Monthly rainfall (mm)",
type = "l",
xlim = as.Date(c("1966-05-01", "1980-12-31")),
(scale_x_date(breaks=date_breaks("5 year"),
labels=date_format("%b %y"))))

You are trying to use the ggplot2 function scale_x_date() with a base R plot.

Using ggplot2:

ggplot(SAWS_Monthly, aes(Date, Sum.of.RainfallMbazwanaA)) +
  geom_line() + 
  scale_x_date(breaks = date_breaks("5 year"),
               labels = date_format("%b %y"),
               limits = as.Date(c("1965-01-01", "1980-01-01"))) +
  labs(x = "Date", y = "Monthly rainfall (mm)")

image

2 Likes

How would I do this using the base R plot and not ggplot2?

I've never done it in base.

Possibly using gap.axis in axis().

https://www.rdocumentation.org/packages/graphics/versions/3.5.2/topics/axis

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.