2

I'm trying to make some stacked bar charts using ggplot2 in R, but no matter what I do my charts always end up being grey.

As a toy example, if I run this:

Year <- c(2015,2015,2015,2015,2016,2016,2017,2017,2017,2017)
Class <- c(2,1,2,3,2,3,1,2,3,2)
Data <- data.frame(Year,Class)

ggplot(Data, aes(Year)) +
  geom_bar(aes(fill=Class))

I get the following (very grey) chart:

Grey Chart

Every ggplot tutorial I have found suggests that I should get something more colourful that looks like a stacked bar chart. Any suggestions? I am a complete novice, so there's a fair chance it's my fault.

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
user10310704
  • 103
  • 1
  • 4
  • 5
    Both `Year` and `Class` should be factors to make this work the way you expect. – Axeman Sep 03 '18 at 14:08
  • Welcome to SO! How do you want to color them? In other words, generally plots are colored with a meaning, not only for fancy purposes: which meaning do you want to give to colors? – s__ Sep 03 '18 at 14:10
  • Possible duplicate of [Change bar plot colour in geom\_bar with ggplot2 in r](https://stackoverflow.com/questions/38788357/change-bar-plot-colour-in-geom-bar-with-ggplot2-in-r) – AmmoPT Sep 03 '18 at 14:12
  • @Axeman or simply character vectors... ;) – tjebo Sep 03 '18 at 14:15
  • Thanks! I want the bars to be split into three differently-coloured sections showing how much of the 'count' comes from each class. I was under the impression that that's what 'fill=Class' did? @Axeman, could you clarify what is meant by 'factors'? I'm very new to this and still collecting terminology :) – user10310704 Sep 03 '18 at 14:17
  • 2
    @AmmoPT, that isn't going to be useful to OP. Clearly he already understand to map `fill` to a variable. Nowhere in that question is it explained that it should be a factor to get that specific result. – Axeman Sep 03 '18 at 14:17
  • 3
    @user10310704, try e.g. `ggplot(Data, aes(factor(Year), fill = factor(Class))) + geom_bar()`. Read [an introductory R text](https://stackoverflow.com/tags/r/info) to understand what a factor is in R. – Axeman Sep 03 '18 at 14:18
  • Thank you very much @Axeman, that works perfectly! Cheers!! – user10310704 Sep 03 '18 at 14:24
  • 1
    @s_t unless you're in marketing – smci Sep 03 '18 at 22:09

1 Answers1

2

use stat='identity' and a table of counts per group. Then use position='stack' to stack bars while preserving grouping information based on the Class

Default stat='count', which means ggplot counts the number of times each value (here, Year), occurs, but it does not take into account any other grouping variables. stat='identity' means ggplot reads the values as actual bar heights.

melt() from reshape2 reformats the table() output into long format which ggplot is able to read

library(ggplot2)
 library(reshape2)
 Year <- c(2015,2015,2015,2015,2016,2016,2017,2017,2017,2017)
 Class <- c(2,1,2,3,2,3,1,2,3,2)
 Data <- data.frame(Year,Class)


Data2 <- melt(table(Data))

 ggplot(mpg, aes(class)) +
  geom_bar(aes(fill = drv))

 ggplot(Data2, aes(x=Year, y=value)) +
  geom_bar(aes(fill = as.factor(Class)), position='stack',  stat='identity')
P1storius
  • 917
  • 5
  • 12