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
This answer does not make use of the possibilities in
ggplot
to transform variables and scales (see @Metric's clean answer), but instead variables are transformed in beforehand.Within each Category, reorder choices based on ratings. Check that 'choices' is a
character
. If it is afactor
, you should convert to character withas.character
, because reordering with a factor as input does not give us what we want (see below).Reverse levels of 'Categories'
Plot
Edit following a comment from @Michael Bellhouse - "it appears alpha category is ranked but not bravo or charlie"
When 'choices' is a character, the factor levels that are generated and reordered in
ddply
is based on each subset of 'choices'. Which works fine. On the other hand, when 'choices' is a factor in the original data, its levels are based on all levels present in the data. Inddply
subset of 'choice' levels are then reordered, but the reordering takes place within the full set of levels. This leads to three sets of conflicting levels and only the first is used.