Home > Mobile >  Need to plot stacked Rank bar graph
Need to plot stacked Rank bar graph

Time:11-01

I need to plot a stacked rank bar graph, in decreasing order like a sample attached graph. I have also linked my data (sample image

I will appreciate your help with this,

Thanks,

CodePudding user response:

You may set levels of year before you plot the graph.

Please notice that names of variable from data attatched are slightly different with your code.

library(tidyverse)
library(ggthemes)

rank_ind<- rank_use %>%
  group_by(year) %>%
  summarize(Water_Use = sum(Water_Use)) %>% arrange(desc(Water_Use)) %>%
  pull(year)
rank_use%>%
  mutate(year = factor(year, levels = rank_ind)) %>%
  ggplot(aes(x = year, y = Water_Use, fill = sector))   
  geom_bar(position = 'stack', stat = 'identity')  
  theme_economist_white()   theme(axis.text.x = element_text(angle = 90), axis.text = 
                                    element_text(face= "bold"),legend.position = 'right')   
  labs(fill = "Sectors", face="bold")  
  ylab('Water Use (Thousand m3)')   xlab("Year")  ylim(0,200000)  # You should be aware of ylim 
  theme(text=element_text(size=10, family="Arial")) 
  ggtitle("Top 10 sectors by water use for 2018, 2019, and with existing  year") 
  theme(plot.title=element_text( hjust=0.5, vjust=0.5, face='bold')) 
  theme(axis.title = element_text(face="bold")) 
  theme(axis.title.x = element_text(margin = unit(c(3, 0, 0, 0), "mm")),
        axis.title.y = element_text(margin = unit(c(0, 3, 0, 0), "mm")),
        plot.title = element_text(vjust = 3)) 
  theme(legend.title = element_text(colour="black", size=10, face="bold"))

enter image description here

  • Related