1
votes

Using the fill argument on geom_bar is not coloring the bars on my plot. I'm using the train.csv from the titanic data set here.

passengers <- read.csv('../input/train.csv')

I have tried moving the fill outside of the aes(), tried moving the aes up to the ggplot() function.

This is the code I'm using on the Titanic Data set

ggplot(data = passengers) + 
    geom_bar(mapping = aes(x=Survived, fill = Pclass))

enter image description here

This is the code I'm using as a template which works fine on the ggplot built in diamonds data.

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = cut))

enter image description here

I just keep getting grey bars with the geom_bar for Survived using Pclass as the fill.

2
It's going to be hard for anyone to help without access to passengers, your data frame of however you've reorganized the Titanic data set. - joran
DOH! Sorry about that. Have edited with the link to the data set and which file I read into the dataframe. - Michael
Please add data directly to your question, not as a link. - erc
is Pclass numeric? if so, try throwing an as.character() around it. - tomasu

2 Answers

2
votes

This is doing the trick for me:

ggplot(data = passengers) + 
   geom_bar(mapping = aes(x=Survived, fill = as.character(Pclass)))

enter image description here

0
votes

You can try as.factor()

ggplot(data = passengers) + 
geom_bar(mapping = aes(x=Survived, fill = as.factor(passengers$Pclass)))

Probably your variables is not factor