0

How can I easily ad one color in each bar and make it descending?

QG4 %>% 
  filter(value=="Yes") %>% 
  ggplot(aes(y=Freq, x=variable))+
  geom_bar(position = "dodge", stat = "identity")+
  theme_bw()+
  coord_flip()+
  labs(x="Mode", y=NULL, title = "What is your usual (or most frequently used) mode of travel to work/place of study?")

I used dput(QG4) to avoid using a picture of the dataset:

structure(list(variable = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("Bicycle", 
"Bicycle (Yélo)", "Bus", "Car", "Car (Yélo)", "Carpool", "Motorcycle/scooter", 
"On foot", "Scooter (trottinette)", "Train"), class = "factor"), 
    value = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("No", 
    "Yes"), class = "factor"), Freq = c(1634L, 2143L, 1781L, 
    1532L, 2281L, 2202L, 2267L, 1331L, 2265L, 2172L, 655L, 146L, 
    508L, 757L, 8L, 87L, 22L, 958L, 24L, 117L)), class = "data.frame", row.names = c(NA, 
-20L))

enter image description here

  • Wesocme to SO! Please help us help you by providing a small sample of your data to make your issue reproducible. See [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for more info on how to provide a reproducible example. – Dan Adams Jan 27 '22 at 19:01
  • Also what do you mean by 'descending'? You mean to sort the bars by value or do you mean the colors? – Dan Adams Jan 27 '22 at 19:03
  • Hello. Thank for welcoming me. I have added a picture of the data and a picture of the plot. By descending, I mean to sort the bars descending. However, I want every bar to get a different color. – Simen Fjælberg Jan 27 '22 at 19:15
  • 1
    Can you please paste your data in a copy-pasteable format using `dput(QG4)`. It's a bad practice to [share images of code/data](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question). It's fine for the graph though, – Dan Adams Jan 27 '22 at 19:22
  • Hello Dan. I have edited my question now using dput(QG4) – Simen Fjælberg Jan 27 '22 at 21:00
  • These are very common issues already answered in the links above. But for your code you could try `QG4 %>% filter(value=="Yes") %>% ggplot(aes(y=Freq, x=reorder(variable, Freq)))+ geom_bar(aes(fill = variable), position = "dodge", stat = "identity", show.legend = F)+ theme_bw()+ coord_flip()+ labs(x="Mode", y=NULL, title = "What is your usual (or most frequently used) mode of travel to work/place of study?")` – Dan Adams Jan 27 '22 at 21:23
  • Thank you Dan. This was very helpful – Simen Fjælberg Jan 27 '22 at 21:31

0 Answers0