简体   繁体   English

如何在任何给定时间根据 dataframe 的最大值设置最大 y 轴限制?

[英]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.我对 R 及其功能相当陌生,所以我会尽力解释我的问题。 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.我目前的最高病例率是 1460,所以当我 plot 图表时,我可以手动将限制设置为 1500 或 2000,这很好。 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).但是,如果我在两个月后回来并且现在的病例率为 330,那么我的代码仍将设置最大值为 2000,这不是很像样(或者,尽管希望不是,但比率会增加到 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 如何扩展 ggplot y 轴限制以包括最大值

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?我的问题是我是否可以将最大 y 值编码为四舍五入,比如说,当时数据集最大值的最接近的 500? For example, if the highest rate is 645, the y-limit will be set at 1000.例如,如果最高速率为 645,则 y 限制将设置为 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.我目前正在使用 scale_y_continuous,据我所知,它的效率可能非常低。 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?)?如果您将这段代码分开,我不会生气,我只需要知道是否有 function(或者您是否可以创建一个?)?

This function:这个 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.如果我理解您的问题,森林生态学家在此线程中( 如何四舍五入到最接近的 10(或 100 或 X)? )似乎有效。

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)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ggplot2在Y轴上设置时间范围(HH:MM:SS)? - How do I set time range (HH:MM:SS) on Y-axis using ggplot2? 如何通过ggplot代码为在图的y轴上显示的值的字符设置最大限制? - How to set a max limit to the characters of the values that are displayed on the y-axis of a plot, through the ggplot code? 如何限制主 y 轴和次 y 轴? - How to limit primary y-axis and secondary y-axis? 为了绘制时间序列 R,如何设置可变的最小和最大 y 轴? - For graphing a time series R, how do I set a variable minimum and maximum y axis? 如何使用概率y轴而不是密度y轴创建直方图? - How do I create a histogram with a probability y-axis rather than a density y-axis? R - 如何根据 Y 轴的值更改线条的颜色? - R - How to change the color of line based on the value of Y-axis? Plotly:如何在辅助 y 轴上设置最小值? - Plotly: How to set a minimum value on secondary y-axis? 如何在ggplot2中设置y轴标签的宽度 - How do I set width of y-axis labels in ggplot2 如何沿图表的 x 轴和 y 轴将度数符号和字母添加到每个值 - How do I add the degree symbol and letters to each value along the x- and y-axis of a graph 如何为辅助 y 轴添加图例? - How do I add a legend for a secondary y-axis?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM