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 figure out sequence problem in bar plot?

data3 <- data.frame(
  Count=c(61,80,47,54,41,92,14,37,33,17,5,53),
  category=c('M','M','M','M','M','M','M','M','F','F','F','F'), 
  Agecate =c('<=5', '6-14', '15-19','above 20', 
             '<=5', '6-14', '15-19','above 20',
             '<=5', '6-14', '15-19','above 20'))

I want to plot count vs category using a bar plot as

ggplot(data3, aes(x = reorder(category,Count), y = Count, fill = Agecate)) +
  geom_bar(stat = "identity",position = "dodge") + theme_bw(base_size = 30)+
  geom_text(aes(label=Count), size=5, position=position_dodge(width=0.9), vjust=-0.25)+
  theme(axis.text.x = element_text(size = 25))+theme(axis.text = element_text(face="bold"))+
  xlab('Woredas')+ylab('Number of hh members') +labs(fill='Age group')

[![enter image description here][1]][1]p

How can I improve this figure on the age group sequence? category 6-14 is coming at the end but must come next to <5. And also some counts labeled twice in the bar plot?

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

>Solution :

Just reorder your variable levels before launching plot, with factor function

data3$Agecate <- factor(data3$Agecate, 
                        levels=c("<=5", "6-14", "15-19", "above 20"))

enter image description here

Concerning double label issue, you have twice each information for ‘M’ category, only keep one or sum them so you only display labels once or create a third group to discriminate them.

The plot only took the first ones into account for bar height.

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