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

发布于 2024-12-07 09:17:55 字数 715 浏览 0 评论 0原文

我正在尝试使用 R 在同一个图上绘制多组有序对。我不需要它们之间的线,因为这已经通过简单的线性回归来处理。下面是一些示例代码:

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

sd 对是 x 坐标,r 对是 y 坐标。我需要在同一个散点图上绘制两组 xy 对。这是简化的数据,但您明白了。

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. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没有伤那来痛 2024-12-14 09:17:55

很抱歉 RTFM 评论的推动。这里有更多细节。

使用基本图形,我将通过如下方式完成您正在做的事情:

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)

我提到点和线的原因是因为您询问如何绘制多组点在同一张图上。 R 中基本图形的一般策略是通过一次调用 plot 来初始化绘图,然后使用诸如 points 之类的东西添加到它、 linesabline 等。

您对 plot.newplot.window 的调用并不是真正必要的;如果您刚刚开始使用 R,您可能暂时不需要使用它们,真的。

一般来说,每次调用plot时,R都会启动一个新的绘图设备。因此,您对 plot 的重复调用只是返回并重新开始。您会注意到,生成的绘图最终的 y 轴限制不是 0 到 12。这是因为每次您重新调用 plot 时,您都会从头开始,就好像之前的命令从未出现过一样发生了。这也是为什么另一组点没有出现的原因。

最后,阅读 ?plot 的建议有点误导,因为实际上 ?plot.default 对于初学者来说信息量更大。它有一些小功能,比如能够直接传递 x 和 y 轴限制,传递 type = "n" 来创建一个具有正确尺寸的空图,然后您可以添加到其中,等等。

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.

郁金香雨 2024-12-14 09:17:55

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

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"))
鹊巢 2024-12-14 09:17:55

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

您可以在 ?panel.xyplot?xyplot 中找到详细信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文