Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show bars in ggplot2 in descending order of a numeric vector?

Tags:

r

ggplot2

rank

df <- data.frame (Categories=c("Alpha Category", "Alpha Category", 
                               "Alpha Category", "Bravo Category", 
                               "Bravo Category", "Bravo Category", 
                               "Charlie Category", "Charlie Category", 
                               "Charlie Category"),  
                  choices=c("alpha1", "alpha2", "alpha3", "bravo1", 
                            "bravo2", "bravo3", "charlie1", "charlie2",
                            "charlie3")  , 
                  ratings=c(20,60,40, 55,75,25,65,35,45))    
df.plot <- ggplot(df, aes(Categories, ratings, fill = choices))
           + geom_bar(position="dodge", stat="identity") 
           + coord_flip()    
df.plot <- df.plot 
           + theme_classic(base_size = 16, base_family = "")  
           + scale_fill_brewer(palette="Paired")    
df.plot <- df.plot 
           + scale_y_continuous(breaks=seq(0,100,by=10),limits=c(0,80) )  
           + ylab("Ratings")  
           + theme(axis.text.y = element_text(size=16)) #change font size of y axis label   
df.plot

Most importantly, I would like to show the "choices" within each "category" in descending order of their "ratings", for example here "Charlie Category" would show charlie1, then charlie3, then charlie2.

I have honestly looked online for solutions for about a week but can't find it. My current thoughts is that I should convert the choices into factors but I haven't figured out how to do this properly.

Of secondary importance, it would be great if the "categories" could be listed, from the top down, "Alpha Category", "Bravo category", "Charlie Category" rather than in the inverse order as seems to occur when coordinates are flipped

like image 451
Michael Bellhouse Avatar asked Sep 15 '13 18:09

Michael Bellhouse


People also ask

How do you arrange bars in descending order in R?

Rearranging Results in Basic R Then draw the bar graph of the new object. If you want the bar graph to go in descending order, put a negative sign on the target vector and rename the object. Then draw the bar graph of the new object.

How do I rearrange bars in ggplot2?

Reordering in ggplot is done using theme() function. Within this, we use axis. text. x with the appropriate value to re-order accordingly.

How do I reorder bars in a Barplot in R?

Another way to re-order barplots is to use the base R function reorder(). It kind of works the same way and we reorder the factors based on population size. Reordering bars in barplot using base R function reorder() also results in the same barplot as we ordered by fct_reorder().

How do I change the width of a bar in ggplot2?

To Increase or Decrease width of Bars of BarPlot, we simply assign one more width parameter to geom_bar() function. We can give values from 0.00 to 1.00 as per our requirements.

Why doesn’t my ggplot2 bar plot have an ascending order?

One of the reasons you’d see a bar plot made with ggplot2 with no ascending / descending order - ordering / arranged is because, By default, ggplot arranges bars in a bar plot alphabetically. But most of the times, it would make more sense to arrange it based on the y-axis it represents (rather than alphabetically).

What is a bar chart in ggplot2?

Bar charts are useful for displaying the frequencies of different categories of data. By default, ggplot2 bar charts order the bars in the following orders: Factor variables are ordered by factor levels. Character variables are order in alphabetical order.

What are reordered factors in a ggplot?

Technically, it’s the data that we’re rearranging (not the bars) but that reordered factors help us in arranged ggplot bars.

How to plot on X and y axis in ggplot2?

As before, we can use geom_col () function in ggplot2 to make a simple barplot. We provide the aesthetics that want to plot on x and y axes from the data and simply add geom_col () layer to it. By default, ggplot2 uses the default factor levels and uses that as order for bars in the barplot.


1 Answers

library(ggplot2)
df.plot <- ggplot(df, aes(x=Categories,y=reorder(choices,ratings), fill = choices)) +
  geom_bar(position = "dodge", stat = "identity") + coord_flip() +
  scale_x_discrete(limits = rev(levels(df$Categories)))

enter image description here

like image 110
Metrics Avatar answered Oct 04 '22 08:10

Metrics