R 编程:创建堆叠条形图,每个堆叠条形具有可变颜色

发布于 2024-08-28 07:24:42 字数 106 浏览 3 评论 0原文

我正在尝试创建一个堆叠条形图,每个堆叠条形图中的颜色可变;也就是说,一个条形图在红色上方显示蓝色,下一个条形图在紫色上方显示红色,等等。我还想保留堆叠图表的功能。非常感谢你们。

亚当

I'm trying to create a stacked bar graph with variable coloring in each stacked bar; that is, one bar has say blue on top of red, the next one red on top of purple, etc. I also wanted to preserve the ability to stack graphs. Thank you so much guys.

Adam

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

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

发布评论

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

评论(1

你爱我像她 2024-09-04 07:24:42

下图(使用上面的代码创建)显示了主要汽车制造商生产的汽车类型。

我将条形高度(实际上是条形段高度)映射到汽车类别;我将条形段颜色映射到汽车制造商。因此,七个 x 轴标签中的每一个都对应于因子“类”中的一个级别;同样,条形段的每种颜色对应于因素“制造商”中的一个级别(“制造商”和“类别”都是“mpg”数据框中的变量/列。最后,y 轴显示汽车数量在每个类别(条形高度)中按制造商(段颜色)

library(ggplot2)
data(mpg)     # data set provided w/ ggplot2

px = ggplot(mpg, aes(x=class, fill=manufacturer)) + geom_bar() 

print(px)

alt text http:// img245.imageshack.us/img245/6678/stackedbar.png

The plot below (which was created w/ the code just above it) shows the type of cars produced by the major car makers.

I mapped bar height (actually bar-segment height) to automobile class; and I mapped bar-segment color to automobile manufacturer. Hence, each of the seven x-axis labels corresponds to one level in the factor 'class'; likewise, each color of the bar segments corresponds to one level in the factor 'manufacturer' (both 'manufacturer' and 'class' are variables/columns w/in the 'mpg' dataframe. Finally, the y axis shows the number of cars in each class (bar height) by manufacturer (segment color).

library(ggplot2)
data(mpg)     # data set provided w/ ggplot2

px = ggplot(mpg, aes(x=class, fill=manufacturer)) + geom_bar() 

print(px)

alt text http://img245.imageshack.us/img245/6678/stackedbar.png

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