如何使用 qplot 绘制多个系列?

发布于 2024-09-03 21:48:25 字数 703 浏览 0 评论 0原文

我试图使用以下数据了解如何在一张图上显示多个系列。

Year <- c('1950', '1960', '1970', '1980')
Bus <- c(10,20,30,40)
Bus.sd <- c(1.1, 2.2, 3.3, 4.4)
Car <- c(20, 20, 40, 40)
Car.sd <- c(1.1, 2.2, 3.3, 4.4)

sample_data = data.frame(Year, Bus, Bus.sd, Car, Car.sd)

qplot(Year, Bus, data=sample_data, geom="pointrange", 
ymin = Bus - Bus.sd/2, ymax = Bus + Bus.sd/2)

例如,使用上述数据,如何在同一图上以不同的颜色显示sample_data$Bus 和sample_data$Car?

我尝试做的是:

p <- qplot(...)

然后

p <- p + qplot(...) 

我复制了上一行,但这给了我一个错误。

我不完全理解 AES 的工作原理。我已经研究了 ggplot2 示例,但很难理解这里的相关示例。或者,如果可以使用此数据制作堆叠条形图(geom_bar),我认为这也可以适当地表示它。

I'm trying to understand how to have more than one series on a plot, using the following data.

Year <- c('1950', '1960', '1970', '1980')
Bus <- c(10,20,30,40)
Bus.sd <- c(1.1, 2.2, 3.3, 4.4)
Car <- c(20, 20, 40, 40)
Car.sd <- c(1.1, 2.2, 3.3, 4.4)

sample_data = data.frame(Year, Bus, Bus.sd, Car, Car.sd)

qplot(Year, Bus, data=sample_data, geom="pointrange", 
ymin = Bus - Bus.sd/2, ymax = Bus + Bus.sd/2)

For example, using the above data, how do I show both sample_data$Bus and sample_data$Car on the same plot in different colors?

What I tried doing was:

p <- qplot(...)

then

p <- p + qplot(...) 

where I replicated the previous line, but this gave me an error.

I don't fully understand how AES works. I have studied the ggplot2 examples, but have difficulty understanding the relevant examples here. Or, if it is possible to make a stacked bar (geom_bar) using this data, I think that would also represent it appropriately.

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

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

发布评论

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

评论(1

新雨望断虹 2024-09-10 21:48:25

我希望这有助于

gplot2 最好地处理长格式的数据,如下所示:

  Year score  sd variable
1 1950    10 1.1      bus
2 1960    20 2.2      bus
3 1970    30 3.3      bus
4 1980    40 4.4      bus
5 1950    20 1.1      car
6 1960    20 2.2      car
7 1970    40 3.3      car
8 1980    40 4.4      car

这会将数据放入 R 中:

data <- structure(list(Year = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 
4L), class = "factor", .Label = c("1950", "1960", "1970", "1980"
)), score = c(10, 20, 30, 40, 20, 20, 40, 40), sd = c(1.1, 2.2, 
3.3, 4.4, 1.1, 2.2, 3.3, 4.4), variable = c("bus", "bus", "bus", 
"bus", "car", "car", "car", "car")), .Names = c("Year", "score", 
"sd", "variable"), row.names = c(NA, -8L), class = "data.frame")

这将使绘图,并闪避全部。您确实需要躲避,因为您的数据是重叠的。您可以使用“W”值控制躲避量。

ggplot(data, aes(x=Year, y=score,col=variable))+
geom_point(position=position_dodge(w=0.2))+
geom_pointrange(aes(ymin=score-sd, ymax=score+sd,group=Year),position=position_dodge(w=0.2))

I Hope this helps

gplot2 works best with data in long format, like so:

  Year score  sd variable
1 1950    10 1.1      bus
2 1960    20 2.2      bus
3 1970    30 3.3      bus
4 1980    40 4.4      bus
5 1950    20 1.1      car
6 1960    20 2.2      car
7 1970    40 3.3      car
8 1980    40 4.4      car

This will get the data into R:

data <- structure(list(Year = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 
4L), class = "factor", .Label = c("1950", "1960", "1970", "1980"
)), score = c(10, 20, 30, 40, 20, 20, 40, 40), sd = c(1.1, 2.2, 
3.3, 4.4, 1.1, 2.2, 3.3, 4.4), variable = c("bus", "bus", "bus", 
"bus", "car", "car", "car", "car")), .Names = c("Year", "score", 
"sd", "variable"), row.names = c(NA, -8L), class = "data.frame")

And this will make the plot, with dodge an all. You properbly need the dodge, because your data is overlapping. You can control the amount of dodging with the "W" value.

ggplot(data, aes(x=Year, y=score,col=variable))+
geom_point(position=position_dodge(w=0.2))+
geom_pointrange(aes(ymin=score-sd, ymax=score+sd,group=Year),position=position_dodge(w=0.2))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文