1

My R code:

1. df <- data.frame(table(mast$State))
2. df 
3. df <- df[order(df$Freq,decreasing = FALSE),]            
4. df
5. df1 <- ggplot(df,aes(x=Var1,y=Freq))+geom_bar(stat="identity")+coord_flip()
6. df1

Line 1 - created new dataframe with freq table of variable 'State' in original dataframe mast - worked fine

Line 2 - executed df to view whether the output is correct - worked fine

Line 3 - arranged the frequency table in increasing order - worked fine

Line 4 - executed df again to check whether data is arranged in an order - worked fine

Line 5 - wanted horizontal bar for dataframe df with varaible 'State'arranged in an order - Not working

why the horizontal bars are not getting arranged in an order though in line 4 freq table was arranged in an order? My final output is: enter image description here

Gowri G
  • 420
  • 4
  • 18
  • 1
    Does this answer your question? [Order Bars in ggplot2 bar graph](https://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph) – dc37 Mar 19 '20 at 04:29
  • Please provide minimal and reproducible example(s) along with the desired output. Use `dput()` for data and specify all non-base packages with `library()` calls. Do not embed pictures for data or code, use indented code blocks instead. – Roger-123 Mar 19 '20 at 04:52

1 Answers1

-1

this should do:

ggplot(df, aes(x = reorder(Var1, Freq), y = Freq)) + 
  geom_bar(stat = "identity") + 
  coord_flip()
phalteman
  • 3,442
  • 1
  • 29
  • 46
Wolfgang Arnold
  • 1,252
  • 8
  • 17