我有4组值:y1,y2,y3,y4和一组x。 y值有不同的范围,我需要将它们绘制为单独的曲线,并在y轴上分别设置不同的值。为了简单起见,我需要3个具有不同值(比例)的y轴来绘制同一个图。在3个y轴的单个绘图中绘制4条曲线
回答
尝试了这一点....
# 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 2011-03-30 19:50:03
@Chase前段时间我从某个人的博客中得到了这个想法,但是我希望我已经保存了这个链接。我在连接点(现在)添加了线条。事实上,我不得不说,这些线条很清楚为什么其他答案在大多数情况下更好。在同一个图上绘制多个图可能会产生误导,如果我们有大致相同的方式增加/减少一组输出,这只是一个很好的方法(即甲烷气和二氧化碳在环境中是我用过的)的情节。 – Rguy 2011-03-30 21:20:53
尝试以下。这看起来并不复杂。一旦你看到正在构建的第一张图,你会发现其他人非常相似。而且,由于有四个相似的图形,您可以轻松地将代码重新配置为反复使用以绘制每个图形的函数。但是,由于我通常使用相同的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这也是一个很好的解决方案。我可能会在某个时候使用它。它比将所有点放在完全相同的图表上的误导性更小。 – Rguy 2011-03-30 00:25:32
如果你想下去学习绘图包超越基本图形的路径,这里使用的变量从@ Rguy的回答与ggplot2
的解决方案:
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所具有的特定问题。 – naught101 2012-07-03 02:12:36
- 1. 如何在MATLAB的同一个绘图窗口中绘制两个不同单位(y轴)的曲线?
- 2. 如何绘制基于Y轴的第二条曲线而第一条曲线基于Y轴?
- 3. 在matlab中绘制一个双y轴的叠加条形图
- 4. PANDAS绘制多个Y轴
- 5. 显示y轴的值的水平线绘制在条形图
- 6. D3只有一个Y轴在右侧绘制折线图
- 7. 如何用4个Y轴绘制图形
- 8. 如何绘制一个巨大的Y轴Coreplot条形图?
- 9. 使用flot绘制双y轴的组合条和折线图
- 10. 绘图曲线在X轴下 - 修复?
- 11. 如何绘制与X和Y的曲线线条与R
- 12. 在grafana中绘制一条曲线
- 13. 在matplotlib中绘制多条曲线/ python
- 14. 在Swing中绘制简单线条图
- 15. 用熊猫和多个y轴绘制
- 16. 在一个图上绘制两条曲线
- 17. R绘制边距和虚线y轴
- 18. 在vb6中绘制一个带有两个轴的线图
- 19. 配置数据以在单个图中绘制多条线图
- 20. 绘制不同轴线类型的多个绘图
- 21. jFreeChart:如何在折线图中绘制y轴
- 22. 在iPhone中绘制曲线?
- 23. 在AndEngine中绘制曲线
- 24. 绘制一条水平线,Y =在matplotlib
- 25. 绘制折线图弯曲
- 26. 在x轴上绘制多条线Highcharts
- 27. 如何在JPanel中使用paintComponent绘制轴线和线条图
- 28. 如何绘制matplotlib中的多个X轴或Y轴?
- 29. 在Python中绘制曲线底图
- 30. Matplotlib:绘制扭曲轴
我不清楚你想要什么。也许这将有助于包含一些ASCII艺术(或图像)模型。此外,你可以看看R图库(http://addictedtor.free.fr/graphiques/RGraphGallery.php)来查看哪一个最接近。 – phooji 2011-03-29 22:53:11