R编程:创建一个堆叠条形图,每个堆叠条形的颜色可变 [英] R programming: creating a stacked bar graph, with variable colors for each stacked bar

查看:267
本文介绍了R编程:创建一个堆叠条形图,每个堆叠条形的颜色可变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在每个堆叠的条中创建一个具有可变颜色的堆叠条形图;也就是说,一个条表示蓝色在红色的顶部,下一个红色在紫色的顶部等。我还想保留堆栈图形的能力。非常感谢你们。

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

推荐答案

(它是由上面的代码创建的)显示了主要汽车制造商生产的汽车类型。

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

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

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)

这篇关于R编程:创建一个堆叠条形图,每个堆叠条形的颜色可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆