开发者

plotting multiple sets of different data points on a graph in R

I'm trying to plot several sets of ordered pairs on the same plot, using R. I don't need a line between them, because that's alrea开发者_开发问答dy taken care of by a simple linear regression. Here's some sample code:

sw_sd <- c(0, 20)
sw_r <- c(5, 10)
aa_sd <- c(0, 16)
aa_r <- c(5, 8)
png("5-airline-cals.png")
plot.new()
plot.window(xlim=c(0,25), ylim=c(0,12))
plot(c(aa_sd, aa_r))
plot(sw_sd,sw_r, pch=22, main="Capital Allocation Lines", xlab="Standard Deviation", ylab="Expected Return")
sw_cal=lm(sw_r~sw_sd)
aa_cal=lm(aa_r~aa_sd)
abline(sw_cal, col="forestgreen", lwd=3)
abline(aa_cal, col="blue", lwd=3)
legend(1, 9, c("Southwest Airlines","American Airlines"), cex=0.8, col=c("forestgreen","blue"), lwd=3);
box()
dev.off()

The sd pairs are the x coordinates, and the r are the y coordinates. I need both sets of x-y pairs on the same scatter plot. This is simplified data, but you get the idea.


Sorry for the drive by RTFM comment. Here's some more detail.

Using base graphics, I would accomplish what you're doing via something like this:

plot(c(sw_sd,aa_sd),c(sw_r,aa_r), pch = 22, 
        col = rep(c('forestgreen','blue'),each = 2),main="Capital Allocation Lines", 
        xlab="Standard Deviation", ylab="Expected Return")
abline(lm(sw_r~sw_sd),col = 'forestgreen',lwd = 3)
abline(lm(aa_r~aa_sd),col = 'blue',lwd = 3)

The reason I mentioned points and lines was because you were asking how to plot multiple sets of points on the same graph. The general strategy with base graphics in R is you initialize a plot with a single call to plot and then you add to it using things like points, lines, abline etc.

Your calls to plot.new and plot.window aren't really necessary; if you're just starting with R you probably won't need to use them for a while, really.

In general, each time you call plot, R will start a new plotting device. So your repeated calls to plot are just going back and starting over again. You'll notice that your resulting plot didn't end up having y axis limits of 0 to 12. That's because each time you called plot anew you were starting over from scratch, as if the previous commands never happened. This is also why the other set of points didn't appear.

Finally, the recommendation to read ?plot was a bit misleading, since really ?plot.default is a bit more informative for beginners. It has little nuggets like being able to pass x and y axis limits directly, passing type = "n" to create an empty plot with the right dimensions that you can then add to, etc.


A quick ggplot-based answer:

dat <- data.frame(sd=c(0,20,0,16),
                  r=c(5,10,5,8),
                  airline=rep(c("Southwest","American"),each=2))

library(ggplot2)
theme_update(theme_bw())
qplot(sd,r,data=dat,colour=airline)+geom_smooth(method="lm")+
  labs(x="Standard Deviation",y="Expected Return")+
  scale_colour_manual(value=c("forestgreen","blue"))


Using the data frame of Ben but with lattice:

library(lattice)
xyplot(r~sd, data=dat, groups=airline,
   type=c('p', 'r'),
   auto.key=list(space='right'),
   main="Capital Allocation Lines",
   xlab="Standard Deviation", ylab="Expected Return")

You will find detailed information in ?panel.xyplot and ?xyplot.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜