gpt4 book ai didi

r - 在具有 3 个 y 轴的单个图中绘制 4 条曲线

转载 作者:行者123 更新时间:2023-12-03 10:48:19 25 4
gpt4 key购买 nike

我有 4 组值:y1、y2、y3、y4 和一组 x。 y 值具有不同的范围,我需要将它们绘制为单独的曲线,在 y 轴上具有单独的一组值。

简单来说,我需要 3 个具有不同值(比例)的 y 轴来绘制同一个图形。

最佳答案

试试这个....

# The data have a common independent variable (x)
x <- 1:10

# Generate 4 different sets of outputs
y1 <- runif(10, 0, 1)
y2 <- runif(10, 100, 150)
y3 <- runif(10, 1000, 2000)
y4 <- runif(10, 40000, 50000)
y <- list(y1, y2, y3, y4)

# Colors for y[[2]], y[[3]], y[[4]] points and axes
colors = c("red", "blue", "green")

# Set the margins of the plot wider
par(oma = c(0, 2, 2, 3))

plot(x, y[[1]], yaxt = "n", xlab = "Common x-axis", main = "A bunch of plots on the same graph",
ylab = "")
lines(x, y[[1]])

# We use the "pretty" function go generate nice axes
axis(at = pretty(y[[1]]), side = 2)

# The side for the axes. The next one will go on
# the left, the following two on the right side
sides <- list(2, 4, 4)

# The number of "lines" into the margin the axes will be
lines <- list(2, NA, 2)

for(i in 2:4) {
par(new = TRUE)
plot(x, y[[i]], axes = FALSE, col = colors[i - 1], xlab = "", ylab = "")
axis(at = pretty(y[[i]]), side = sides[[i-1]], line = lines[[i-1]],
col = colors[i - 1])
lines(x, y[[i]], col = colors[i - 1])
}

# Profit.

Plot Output

关于r - 在具有 3 个 y 轴的单个图中绘制 4 条曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5479822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com