0

I want to develop a bar plot using this data with the same of columns as shown in the data.

Valley Female Young Yearling Class1 Class2 Class3
Hushey 144 160 162 24 18 29

I tried this code, it produce the bar plot but with changed order of columns. developing bar plot using pivot_longer function, which produced graph but also changed the order of columns.

ibex %>%
  pivot_longer(cols = -Valley) %>%
  ggplot(aes(x=name,y=value)) +
  geom_col() +
  facet_wrap(~Valley) +
  scale_y_continuous(expand = expansion(0)) +
  theme_bw() +
  labs(title = "", x="", y="Number of Ibex sighted") 
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jan 25 '23 at 13:36
  • I am using this code to develop the bar plot: ibex %>% pivot_longer(cols = -Valley) %>% ggplot(aes(x=name,y=value))+ geom_col()+ facet_wrap(~Valley)+ scale_y_continuous(expand = expansion(0))+ theme_bw()+ labs(title = "",x="",y="Number of Ibex sighted") – Hussain Ali Jan 25 '23 at 13:38
  • Please [edit] your question and put the code there in a code block (see https://stackoverflow.com/editing-help and https://meta.stackexchange.com/a/22189 for formatting in questions). Code in comments formats poorly, and comments are often skipped by readers and/or hidden (when many) by the Stack interface. (I'll edit your code in for now, please do it yourself in the future.) Thanks! – r2evans Jan 25 '23 at 13:42
  • If you want to change the order of things in ggplot2, it relies very much on the lexicographic (alphabetic) sort of strings. To control the order, use `factor`, such as `ibex %>% pivot_longer(cols = -Valley) %>% mutate(name = factor(name, levels=name))` (and then your plotting code). – r2evans Jan 25 '23 at 13:46

0 Answers0