Home > Software engineering >  How to figure out sequence problem in bar plot?
How to figure out sequence problem in bar plot?

Time:08-15

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?

CodePudding user response:

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.

  • Related