在3个y轴的单个绘图中绘制4条曲线 [英] Plot 4 curves in a single plot with 3 y-axes
本文介绍了在3个y轴的单个绘图中绘制4条曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有4组值:y1,y2,y3,y4和一组x。 y值有不同的范围,我需要将它们绘制为单独的曲线,并在y轴上分别设置一组值。简单来说,我需要3个具有不同值(尺度)的y轴用于绘制同一图形。 试试这个....
#数据有一个共同的自变量(x)
x < - 1:10
#生成4组不同的输出
y1 < - runif(10,0,1)
y2 < - runif(10,100,150)
y3 < - runif (10,1000,2000)
y4 < - runif(10,40000,50000)$ b $由< - 列表(y1,y2,y3,y4)
#颜色对于y [[2]],y [[3]],y [[4]]点和轴
colors = c(red,blue,green)
#设置图的边距更宽
par(oma = c(0,2,2,3))
plot(x,y [[1]],yaxt = n,xlab =Common x-axis,main =一堆图在同一个图上,
ylab =)
lines(x,y [[1]])
#我们使用漂亮功能去生成漂亮的轴
轴(at = pretty(y [[1]]),side = 2)
#轴的边。下一个将继续在
#的左边,以下两个在右边
sides< - list(2,4,4)
#数字 (2:4){
par(new = 2),
lines < - list(2,NA,2) TRUE)
plot(x,y [[i]],axes = FALSE,col = colors [i - 1],xlab =,ylab =)
轴(at = pretty y [i]],side = sides [[i-1]],line = lines [[i-1]],
col = colors [i - 1])$ b $ b lines(x ,y [[i]],col = colors [i - 1])$ b $ b}
#利润。
I have 4 sets of values: y1, y2, y3, y4 and one set x. The y values are of different ranges, and I need to plot them as separate curves with separate sets of values on the y-axis.
To put it simple, I need 3 y-axes with different values (scales) for plotting on the same figure.
解决方案
Try this out....
# 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.
这篇关于在3个y轴的单个绘图中绘制4条曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文