我想要比较两条曲线,可以用R画一张图,然后在上面画另一张图?怎么做到的?
谢谢。
发布于 2010-12-15 06:07:34
使用基数R,您可以绘制一条曲线,然后使用lines()参数添加第二条曲线。下面是一个简单的例子:
x <- 1:10
y <- x^2
y2 <- x^3
plot(x,y, type = "l")
lines(x, y2, col = "red")或者,如果您想使用ggplot2,这里有两种方法-一种在同一绘图上绘制不同的颜色,另一种为每个变量生成单独的绘图。这里的诀窍是首先将数据“熔化”为长格式。
library(ggplot2)
df <- data.frame(x, y, y2)
df.m <- melt(df, id.var = "x")
qplot(x, value, data = df.m, colour = variable, geom = "line")
qplot(x, value, data = df.m, geom = "line")+ facet_wrap(~ variable)发布于 2010-12-15 16:41:34
使用lattice package
require(lattice)
x <- seq(-3,3,length.out=101)
xyplot(dnorm(x) + sin(x) + cos(x) ~ x, type = "l")

发布于 2010-12-15 19:35:51
已经为您提供了一些解决方案。如果你继续使用基础包,你应该熟悉plot(), lines(), abline(), points(), polygon(), segments(), rect(), box(), arrows(), ...的函数,看看他们的帮助文件。
您应该会看到基本包中的一个绘图,它是一个窗格,其中包含您给出的坐标。在该窗格上,您可以使用上述函数绘制一整套对象。它们允许你按照你想要的方式构建图形。您应该记住,除非您使用Dr. G所示的par设置,否则每次调用plot()都会给您一个新的窗格。还要考虑到事情可以被绘制在其他事情上,所以考虑一下你用来绘制事情的顺序。
参见例如:
set.seed(100)
x <- 1:10
y <- x^2
y2 <- x^3
yse <- abs(runif(10,2,4))
plot(x,y, type = "n") # type="n" only plots the pane, no curves or points.
# plots the area between both curves
polygon(c(x,sort(x,decreasing=T)),c(y,sort(y2,decreasing=T)),col="grey")
# plot both curves
lines(x,y,col="purple")
lines(x, y2, col = "red")
# add the points to the first curve
points(x, y, col = "black")
# adds some lines indicating the standard error
segments(x,y,x,y+yse,col="blue")
# adds some flags indicating the standard error
arrows(x,y,x,y-yse,angle=90,length=0.1,col="darkgreen")这为您提供了:

https://stackoverflow.com/questions/4444727
复制相似问题