开发者

R programming: creating a stacked bar graph, with variable colors for each stacked bar

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 on开发者_如何学编程e 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.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜