R: Create a bar graph with variable colors for each stacked row

I am trying to create a stacked bar chart with variable colors in each stacked panel; those. one bar says blue over red, the next red over purple, etc. I also wanted to keep the ability to stack graphs. Thank you very much.

Adam

+2


source to share


1 answer


The graph below (which was created with the code above it) shows the type of vehicles produced by the major automakers.

I have shown the height of the bar (actually the height of the bar segment) for the automotive class; and I matched the color of the bar segment with the car manufacturer. Therefore, each of the seven X-axis labels corresponds to one level in the factor class; Likewise, each color of the dash segments corresponds to one level in the "manufacturer" factor (both "manufacturer" and "class" are variables / columns with the "mpg" framework. 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

+3


source







All Articles