Home > front end >  how to have the legend inside a grouped bar graph in R ggplot?
how to have the legend inside a grouped bar graph in R ggplot?

Time:11-09

So my legend here is village which has (Chirodzo, God, Ruaca). How to remove the legend and display it inside the bars; for instance inside the bar for chirodzo, I want chirodzo written inside?

ggplot(data = interviews_plotting, aes(x = respondent_wall_type, fill = village))  
    geom_bar(position = "fill")

Source is here enter image description here

Or, if you want to get a bit more sophisticated with your label placement and theme choices:

library(ggplot2)

ggplot(interviews_plotting, aes(x = respondent_wall_type, fill = village))  
  geom_bar(position = position_dodge(width = 0.9), width = 0.8)  
  geom_text(stat = 'count', size = 6,
            aes(y = ifelse(stat(count) > 2, stat(count)/2, stat(count)),
                label = village, group = village, 
                hjust = ifelse(stat(count) > 2, 0.5, -0.2)), 
            position = position_dodge(width = 0.9), angle = 90)  
  labs(x = 'Wall type', y = 'Count')  
  theme_minimal(base_size = 16)  
  scale_fill_brewer(palette = 'Set2', guide = 'none')

enter image description here


Data used

interviews_plotting <- read.csv(paste0("https://raw.githubusercontent.com/",
                                       "humburg/r-ggplot-project/master/",
                                       "data_output/interviews_plotting.csv"))
  • Related