0
temp1<- data.frame(x =(1:10), y=(1:10)^2)

temp2<- data.frame(x =(1:10), y=(1:10)^3)

# plot(temp1$x, with both temp1$y and temp2$y; 
# want each represented by a different color)

是否有可能做到这一点?

4

4 回答 4

3
plot(temp2, type="l", col="green")
lines(temp1, col="red")
于 2012-04-13T06:52:17.917 回答
2
matplot(temp1$x, cbind(temp1$y, temp2$y), t="l", lty=1, col=c("red", "blue"))

or

library(ggplot2)
qplot(x, y, colour=which, geom="path", data=lattice::make.groups(temp1, temp2))
于 2012-04-13T06:51:29.117 回答
1

或者,您可以使用 ggplot2 实现此目的。假设您的数据集如下所示:

x      y   category
1      3   A
3.2   4   B

您可以使用以下方法绘制两条具有不同颜色的线:

ggplot(aes(x=x, y=y, color=category), data = dat) + geom_line()
于 2012-04-13T07:01:04.010 回答
0

Yep, it is. See ?plot, and the col (colour) argument for the colour.

As to getting them both on the same plot you can either use lines/points (which draw on the existing plot) or see ?par and the new option.

In particular, par(new=TRUE) doesn't clean the current plot device, allowing you to draw on top (a bit counter-intuitive, I know).

So:

# plot temp1 y vs x in blue
plot(y~x, temp1, col='blue')

# draw the next plot on the same plot
par(new=TRUE)

# plot temp2 y vs x in red, on the SAME plot (new=TRUE)
plot(y~x, temp2, col='red')

If you wanted to use lines/points, instead of doing the par(new=TRUE) and second plot, just do lines(y~x,temp2,...)

于 2012-04-13T06:52:43.403 回答