【问题标题】:Plot 4 curves in a single plot with 3 y-axes在具有 3 个 y 轴的单个图中绘制 4 条曲线
【发布时间】:2011-03-29 22:46:47
【问题描述】:

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

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

【问题讨论】:

  • 我不清楚你到底想要什么。也许它会有助于包含一些 ASCII 艺术(或图像)模型来展示它应该是什么样子。此外,您还可以查看 R 图形库 (addictedtor.free.fr/graphiques/RGraphGallery.php),看看哪个最接近。

标签: r graphics


【解决方案1】:

试试这个....

# 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.

【讨论】:

  • 这里展示的 R-fu 给我留下了深刻的印象,但我个人无法从结果产品中脱颖而出。如果除了/替换颜色之外还使用了不同的符号,可能会更清楚。或者它可能只是显示的随机数据的产物......无论如何 - 干得好! (+1)
  • @Chase 我不久前从某人的博客中得到了这个想法,但我希望我保存了链接。我在连接点的图中添加了线条(刚刚)。实际上,我不得不说,这些线条非常清楚地说明了为什么在大多数情况下其他答案更好。在同一个图上绘制多个图可能会产生误导,如果我们有一组输出以大致相同的方式增加/减少(即环境中的甲烷气体和二氧化碳就是我用过的),这将是一个好方法某种情节)。
【解决方案2】:

如果您想在基础图形之外学习绘图包,这里有一个解决方案,ggplot2 使用来自@Rguy 答案的变量:

library(ggplot2)
dat <- data.frame(x, y1, y2, y3, y4)

dat.m <- melt(dat, "x")

ggplot(dat.m, aes(x, value, colour = variable)) + geom_line() +
facet_wrap(~ variable, ncol = 1, scales = "free_y") +
scale_colour_discrete(legend = FALSE)

【讨论】:

  • 这是一个比此类数据接受的答案更合乎逻辑的布局,但它实际上取决于 OP 的具体问题..
  • 非常感谢您的评论!但是,我需要在 aes 部分添加 group=1 。否则它会抛出“geom_path:每个组仅包含一个观察值。您需要调整组审美吗?”错误。
【解决方案3】:

试试下面的。它并不像看起来那么复杂。一旦您看到正在构建的第一个图表,您会发现其他图表非常相似。而且,由于有四个相似的图表,您可以轻松地将代码重新配置为一个函数,该函数反复用于绘制每个图表。但是,由于我通常使用相同的 x 轴绘制各种图形,因此我需要很大的灵活性。所以,我决定复制/粘贴/修改每个图表的代码会更容易。

#Generate the data for the four graphs
x <- seq(1, 50, 1)
y1 <- 10*rnorm(50)
y2 <- 100*rnorm(50)
y3 <- 1000*rnorm(50)
y4 <- 10000*rnorm(50)

#Set up the plot area so that multiple graphs can be crammed together
par(pty="m", plt=c(0.1, 1, 0, 1), omd=c(0.1,0.9,0.1,0.9))

#Set the area up for 4 plots
par(mfrow = c(4, 1))

#Plot the top graph with nothing in it =========================
plot(x, y1, xlim=range(x), type="n", xaxt="n", yaxt="n", main="", xlab="", ylab="")
mtext("Four Y Plots With the Same X", 3, line=1, cex=1.5)

#Store the x-axis data of the top plot so it can be used on the other graphs
pardat<-par()
xaxisdat<-seq(pardat$xaxp[1],pardat$xaxp[2],(pardat$xaxp[2]-pardat$xaxp[1])/pardat$xaxp[3])

#Get the y-axis data and add the lines and label
yaxisdat<-seq(pardat$yaxp[1],pardat$yaxp[2],(pardat$yaxp[2]-pardat$yaxp[1])/pardat$yaxp[3])
axis(2, at=yaxisdat, las=2, padj=0.5, cex.axis=0.8, hadj=0.5, tcl=-0.3)
abline(v=xaxisdat, col="lightgray")
abline(h=yaxisdat, col="lightgray")
mtext("y1", 2, line=2.3)
lines(x, y1, col="red")

#Plot the 2nd graph with nothing ================================
plot(x, y2, xlim=range(x), type="n", xaxt="n", yaxt="n", main="", xlab="", ylab="")

#Get the y-axis data and add the lines and label
pardat<-par()
yaxisdat<-seq(pardat$yaxp[1],pardat$yaxp[2],(pardat$yaxp[2]-pardat$yaxp[1])/pardat$yaxp[3])
axis(2, at=yaxisdat, las=2, padj=0.5, cex.axis=0.8, hadj=0.5, tcl=-0.3)
abline(v=xaxisdat, col="lightgray")
abline(h=yaxisdat, col="lightgray")
mtext("y2", 2, line=2.3)
lines(x, y2, col="blue")

#Plot the 3rd graph with nothing =================================
plot(x, y3, xlim=range(x), type="n", xaxt="n", yaxt="n", main="", xlab="", ylab="")

#Get the y-axis data and add the lines and label
pardat<-par()
yaxisdat<-seq(pardat$yaxp[1],pardat$yaxp[2],(pardat$yaxp[2]-pardat$yaxp[1])/pardat$yaxp[3])
axis(2, at=yaxisdat, las=2, padj=0.5, cex.axis=0.8, hadj=0.5, tcl=-0.3)
abline(v=xaxisdat, col="lightgray")
abline(h=yaxisdat, col="lightgray")
mtext("y3", 2, line=2.3)
lines(x, y3, col="green")

#Plot the 4th graph with nothing =================================
plot(x, y4, xlim=range(x), type="n", xaxt="n", yaxt="n", main="", xlab="", ylab="")

#Get the y-axis data and add the lines and label
pardat<-par()
yaxisdat<-seq(pardat$yaxp[1],pardat$yaxp[2],(pardat$yaxp[2]-pardat$yaxp[1])/pardat$yaxp[3])
axis(2, at=yaxisdat, las=2, padj=0.5, cex.axis=0.8, hadj=0.5, tcl=-0.3)
abline(v=xaxisdat, col="lightgray")
abline(h=yaxisdat, col="lightgray")
mtext("y4", 2, line=2.3)
lines(x, y4, col="darkgray")

#Plot the X axis =================================================
axis(1, at=xaxisdat, padj=-1.4, cex.axis=0.9, hadj=0.5, tcl=-0.3)
mtext("X Variable", 1, line=1.5)

下面是四张图的图。

【讨论】:

  • + 1 这也是一个很好的解决方案。我可能会在某个时候使用它。这比将所有点放在完全相同的图表上更容易产生误导。
猜你喜欢
  • 2010-12-15
  • 2021-07-18
  • 2015-05-18
  • 2021-11-13
  • 2018-08-15
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
相关资源
最近更新 更多