简体   繁体   中英

How do I set a max y-axis limit based on the maximum value of a dataframe at any given time?

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 (ie 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?)?

This function:

round.choose <- function(x, roundTo, dir = 1) {
  if(dir == 1) {  ##ROUND UP
    x + (roundTo - x %% roundTo)
  } else {
    if(dir == 0) {  ##ROUND DOWN
      x - (x %% roundTo)
    }
  }
}

by forestecologist in this thread ( How to round up to the nearest 10 (or 100 or X)? ) seems to work, if I've understood your question.

df <- data.frame(x = c(0,1,2,3,4,5),
                 y = c(100, 150, 200, 250, 300, 350))

ggplot(data = df, aes(x = x, y = y)) +
  geom_point() +
  scale_y_continuous(limits = c(0, round.choose(max(df$y), 500, 1)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM