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
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.
Reordering in ggplot is done using theme() function. Within this, we use axis. text. x with the appropriate value to re-order accordingly.
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().
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.
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).
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.
Technically, it’s the data that we’re rearranging (not the bars) but that reordered factors help us in arranged ggplot bars.
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.
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)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With