Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can't I color my bar chart when I make it show percentages?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 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()

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading