I'm fairly new to R and its functionality, so I will try my best to explain my issue. I am plotting a simple enough line graph with weekly epidemiological data. As far as I'm aware, my code currently downloads the data, and manipulates it in a way that allows new data to be added (i.e. I could run the code in a few months time and it would have added the weeks I had missed). My highest case rate at the moment is 1460, so when I plot the graph, I can manually set the limit at 1500 or 2000, which is fine. However, if I come back in two months time and the case rate is now 330, then my code will still set a max of 2000, which is not very presentable (or, although hopefully not, the rate increases to say 3300). Ideally, I don't want to be changing the limit manually each time.
I did find a similar question and answer but I didn't fully understand the log aspect.
How to expand ggplot y axis limits to include maximum value
My question here is whether or not I can code the max y-vale to be rounded up to, lets say, the nearest 500 of the maximum value of the dataset at the time? For example, if the highest rate is 645, the y-limit will be set at 1000.
Region_Case_Graph <-ggplot(data = Region_Weekly_Cases_Long,
aes(x = date, y = Weekly_Cases, color = Local_Authority)) +
geom_line() +
geom_point() +
xlab("Date\n") + ylab("Weekly Cases\n") +
scale_x_date(date_breaks = "1 month",
date_labels = "%d-%b-%y") +
theme(axis.text.x = element_text(size = 10, angle = 90, vjust = 0.25)) +
labs(color = "Local Authority\n") +
theme(panel.background = element_rect(fill = "white"),
panel.grid.major = element_line(size = 0.5, colour = "lightgrey")) +
scale_y_continuous(limits = c(0,2000)) +
ggtitle("Weekly Cases in each Local Authority, 07/20 to Present\n")
I'm currently using scale_y_continuous, which for all I know may be incredibly inefficient. I will not be offended if you pull this code apart, I just need to know if there's a function (or whether you could potentially create one?)?