简体   繁体   English

在R中的图形上绘制多组不同的数据点

[英]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 already taken care of by a simple linear regression. 我正在尝试使用R在同一图上绘制几组有序对。我不需要它们之间的线,因为已经通过简单的线性回归处理了。 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. sd对是x坐标, r是y坐标。 I need both sets of xy pairs on the same scatter plot. 我需要在同一散点图上同时使用两组xy对。 This is simplified data, but you get the idea. 这是简化的数据,但是您可以理解。

Sorry for the drive by RTFM comment. 对不起,RTFM评论的驱动器。 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. 我提到pointslines的原因是因为您要问如何在同一张图上绘制多组点。 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. R中具有基本图形的一般策略是,您只需调用一次绘图就可以初始化plot ,然后使用pointslinesabline lines添加abline

Your calls to plot.new and plot.window aren't really necessary; 您实际上不需要调用plot.newplot.window if you're just starting with R you probably won't need to use them for a while, really. 如果您只是从R开始,那么可能真的不需要一段时间使用它们。

In general, each time you call plot , R will start a new plotting device. 通常,每次调用plot ,R都会启动一个新的绘图设备。 So your repeated calls to plot are just going back and starting over again. 因此,您对plot的重复调用只是返回并重新开始。 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. 您会注意到,最终的绘图并没有以y轴限制为0到12。这是因为每次您重新调用plot时,都是从头开始,就像以前的命令从未发生过一样。 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. 最后,阅读?plot的建议有点误导,因为真正的?plot.default对初学者来说更?plot.default 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. 它几乎没有什么小功能,例如能够直接通过x和y轴限制,通过type = "n"创建具有正确尺寸的空图,然后可以添加到其中,等等。

A quick ggplot -based answer: 基于ggplot的快速答案:

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: 使用Ben的数据框但带有晶格:

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 . 您可以在?panel.xyplot?xyplot找到详细的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM