With the code written below I do get the output to look exactly how I want it to, but if I were to add on the y=..prop.. and group = 1 argument it wont’t run if fill = TicketClass is left. But if I remove that it just results in a grey bar chart. Is there any way to fix this?
This is how I wrote it first and it ran fine:
Titanic %>%
ggplot(aes(x = TicketClass,
fill = TicketClass)) +
geom_bar() +
scale_fill_manual(values = wes_palette(n = 3,
name = "Moonrise3")) +
labs(x = "Ticket Class",
y = "Number of Passengers",
title = "Passengers Aboard the Titanic.",
caption = "Data from the Titanic R Package.") +
theme_bw()
To show percentages/proportions I’ve written it like this, and it won’t run:
Titanic %>%
ggplot(aes(x = TicketClass,
y = ..prop..,
group = 1,
fill = TicketClass)) +
geom_bar() +
scale_fill_manual(values = wes_palette(n = 3,
name = "Moonrise3")) +
labs(x = "Ticket Class",
y = "Number of Passengers",
title = "Passengers Aboard the Titanic.",
caption = "Data from the Titanic R Package.") +
theme_bw()
What am I doing wrong and how can I go about fixing this?
>Solution :
Note I have used titanic dataset from here: https://rpubs.com/kelvinsonmwangi/645367 using Pclass
replace the fill aesthetics by fill(..x..):
library(ggplot)
library(wesanderson)
titanic %>%
ggplot(aes(x = Pclass,
y = ..prop..,
group = 1,
fill = factor(..x..))) +
geom_bar() +
scale_fill_manual(values = wes_palette(n = 3,
name = "Moonrise3")) +
labs(x = "Ticket Class",
y = "Number of Passengers",
title = "Passengers Aboard the Titanic.",
caption = "Data from the Titanic R Package.") +
theme_bw()
