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"))

また

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

次を使用して、異なる色の 2 つの線をプロットできます。

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

ええ、そうです。?plotおよび色のcol(color) 引数を参照してください。

両方を同じプロットに表示するには、 lines/ points(既存のプロットに描画する) または see?parnewオプションを使用できます。

特に、現在のプロット デバイスをクリーンアップpar(new=TRUE) せず、上に描画できるようにします (少し直感に反しますが、私は知っています)。

そう:

# 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')

lines/を使用したい場合は、 and secondpointsを実行する代わりに、単に実行しますpar(new=TRUE)plotlines(y~x,temp2,...)

于 2012-04-13T06:52:43.403 に答える