简体   繁体   English

使用多个分布函数在 R 图形上修复 y 轴标签

[英]Fixing y-axis labels on R graph with several distribution functions

在此处输入图像描述

I am trying to graph a few different gamma distribution functions in R with the standard R commands--no packages.我正在尝试使用标准 R 命令在 R 中绘制一些不同的伽马分布函数——无包。

As you can see here, the y-axis is being redone for each function I graph.正如您在此处看到的,正在为每个 function I 图形重做 y 轴。 Is there a way that I can have all 7 of my functions go along the same y-axis?有没有一种方法可以让我的所有 7 个功能 go 沿同一 y 轴? Here is my code这是我的代码

par(mfrow=c(1,1))
x <- seq(0, 1, length = 10000)
fun1 <- function(x) dgamma(x, 1, 2)
fun2 <- function(x) dgamma(x, 2, 2)
fun3 <- function(x) dgamma(x, 3, 2)
fun4 <- function(x) dgamma(x, 5, 1)
fun5 <- function(x) dgamma(x, 9, .5)
fun6 <- function(x) dgamma(x, 7.5, 1)
fun7 <- function(x) dgamma(x, .5, 1)

plot(fun1, 0, 20, col = "red")
par(new = TRUE)
plot(fun2, 0, 20, col = "orange")
par(new = TRUE)
plot(fun3, 0, 20, col = "yellow")
par(new = TRUE)
plot(fun4, 0, 20, col = "green")
par(new = TRUE)
plot(fun5, 0, 20, col = "black")
par(new = TRUE)
plot(fun6, 0, 20, col = "blue")
par(new = TRUE)
plot(fun7, 0, 20, col = "purple")

Rather than using S3 dispatch to call plot.function() , I would prefer to directly call curve() , and use the add parameter rather than par(new = TRUE) to add lines to the existing plot;与其使用 S3 dispatch 调用plot.function() ,不如直接调用curve() ,并使用add参数而不是par(new = TRUE)向现有 plot 添加行; then we get just one set of axis and tick labels for the y axis:然后我们只得到一组 y 轴的轴和刻度标签:

curve(fun1, 0, 20, col = "red")
curve(fun2, 0, 20, col = "orange", add = TRUE)
curve(fun3, 0, 20, col = "yellow", add = TRUE)
curve(fun4, 0, 20, col = "green", add = TRUE)
curve(fun5, 0, 20, col = "black", add = TRUE)
curve(fun6, 0, 20, col = "blue", add = TRUE)
curve(fun7, 0, 20, col = "purple", add = TRUE)

在此处输入图像描述

As you can see, this is quite different than the plot you initially had, which is because it was re-drawing the axes each time instead of sticking to one set of axis limits.如您所见,这与您最初拥有的 plot 完全不同,这是因为它每次都重新绘制轴,而不是坚持一组轴限制。

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

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