Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to reorder categories on ggplot2 axis

I am trying to reorder categories on my ggplot2 axis so that they are in the order Dark, Medium, Light instead of in alphabetical order.

I have tried to reorder them, and the bars show the correct orders, but the labels of Medium and Light have not switched to reflect the correct order.

Here is some reproducible code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df = data.frame(Group = c(rep('A',6), rep('B',6), rep('C',6)),
               Status = c('0', '0', '0', '1', '1', '1', '0', '0', '0', '1', '1', '1','0', '0', '0', '1', '1', '1'),
               Color = c('Dark','Light','Medium','Dark','Light','Medium','Dark','Light','Medium','Dark','Light','Medium','Dark','Light','Medium','Dark','Light','Medium'),
               Count = c(3,6,9,3,4,1,5,2,7,3,4,8,5,3,7,6,2,6))

df <- df %>% group_by(Group, Status) %>% dplyr::mutate(percent = round(Count*100/sum(Count), 1))


ggplot(data = df, aes(x = Color, fill = Group, alpha= Status)) +
 scale_alpha_manual(values = c(1, 0.5)) +
 geom_bar(data=df[df$Status=="1",], aes(y=percent), stat="identity") +
 geom_bar(data=df[df$Status=="0",], aes(y=-percent), stat="identity") +
 scale_x_discrete(labels = df$Color) +
 coord_flip(ylim=c(-80,80)) +
 theme(text = element_text(size=16)) +
 scale_y_continuous(breaks=seq(-80,80,20),labels=abs(seq(-80,80,20))) + 
 scale_fill_brewer(palette="Set2") +
 facet_wrap(~Group, ncol=1) + theme(legend.position="right") +
 labs(y="Percentage", x="Colour", fill="Group", alpha = "Status") +
 theme_light()

Here is the Plot produced:

Plot

I’ve tried to reorder the categories using a couple methods such as adding limits

scale_x_discrete(labels = df$Color, limits = c("Dark", "Medium", "Light"))

and creating another column with reordered categories.

df$Color2 <- factor(df$Color, level = c('Dark', 'Medium', 'Light'))

What I am getting is plots represented the way I want them to be leveled, but with the labels Medium and Light as is instead of being switched:

Plot with inaccurate labels

How can I reorder my categories and get the labels to represent what they should represent?

>Solution :

Is this what you want?

df$Color <- factor(df$Color, levels=c("Light", "Medium", "Dark"))

ggplot(data = df, aes(x = Color, fill = Group, alpha= Status)) +
  scale_alpha_manual(values = c(1, 0.5)) +
  geom_bar(data=df[df$Status=="1",], aes(y=percent), stat="identity") +
  geom_bar(data=df[df$Status=="0",], aes(y=-percent), stat="identity") +
  #scale_x_discrete(labels = df$Color) +
  coord_flip(ylim=c(-80,80)) +
  theme(text = element_text(size=16)) +
  scale_y_continuous(breaks=seq(-80,80,20),labels=abs(seq(-80,80,20))) + 
  scale_fill_brewer(palette="Set2") +
  facet_wrap(~Group, ncol=1) + theme(legend.position="right") +
  labs(y="Percentage", x="Colour", fill="Group", alpha = "Status") +
  theme_light()

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading