Two ways of constructing a complicated bar chart

Last time, we constructed this chart for the titanic dataset:

titanic <- read.csv("http://guerzhoy.princeton.edu/201s20/titanic.csv")
titanic$Pclass <- as.character(titanic$Pclass)
titanic$cSurvived <- ""
titanic$cSurvived[titanic$Survived == 0] <- "Died"
titanic$cSurvived[titanic$Survived == 1] <- "Survived"
t1 <- titanic %>% group_by(Pclass, cSurvived) %>% 
                  summarize(num = n()) %>% 
                  ungroup %>% 
                  mutate(p = num/sum(num))

ggplot(data = t1, mapping = aes(x = cSurvived)) + 
            geom_bar(stat = "identity", position = "stack", mapping = aes(y = p, fill = Pclass) ) + xlab("")

Here is another way to accomplish the same thing:

ggplot(titanic) + 
  geom_bar(mapping = aes(x = cSurvived, y = ..count../nrow(titanic), fill = Pclass), stat = "count")

The default stat for geom_bar is "count". stat transforms the data: it creates the special values ..count.. and ..prop.. that we can use. stat identity leaves the data as is. That is why we needed to pre-transform the data before displaying it.

Recall other possibilities with stat = ..count..:

ggplot(titanic) + 
  geom_bar(mapping = aes(x = cSurvived, y = ..prop.., fill = Pclass), stat = "count")

ggplot(titanic) + 
  geom_bar(mapping = aes(x = cSurvived, y = ..prop.., fill = Pclass, group = 1), stat = "count")

ggplot(titanic) + 
  geom_bar(mapping = aes(x = cSurvived, y = ..prop.., fill = Pclass, group = Pclass), stat = "count")

ggplot(titanic) + 
  geom_bar(mapping = aes(x = cSurvived, y = ..prop.., fill = Pclass, group = Pclass), stat = "count", position = "dodge")

ggplot(titanic) + 
  geom_bar(mapping = aes(x = cSurvived, y = ..prop.., fill = Pclass, group = Pclass), stat = "count", position = "identity")

ggplot(titanic) + 
  geom_bar(mapping = aes(x = cSurvived, y = ..prop.., fill = Pclass, group = Pclass), stat = "count", position = "identity", alpha = 0.4)