0

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 https://mq-software-carpentry.github.io/r-ggplot-extension/02-categorical-data/index.html

ggplot(data = interviews_plotting, aes(x = respondent_wall_type, fill = village)) +
    geom_bar(position = "fill")
lo lo
  • 1
  • 1

2 Answers2

1

To label your bars with the fill category and getting rid of the legend you could use geom_text like so:

Using mtcars as example data:

library(ggplot2)

ggplot(data = mtcars, aes(x = am, fill = factor(cyl))) +
  geom_bar(position = "fill") +
  geom_text(aes(label = cyl), stat = "count", position = position_fill(vjust = 0.5)) +
  guides(fill = "none")

stefan
  • 90,330
  • 6
  • 25
  • 51
  • So I am using grouped bar charts and not stacked and my text is pretty long and I want it to be displayed vertically and not horizontally, like the angle should be 90 – lo lo Nov 08 '22 at 15:33
  • Hm. Unfortunately the information that you are "using grouped bar charts and not stacked" is not given in your question. You example code and the link you referenced in your comment is using `position="fill"`, i.e. a stacked barchart. But basically that doesn't matter. The approach is the same. And if you want a solution which is more suitable to your real case then I would suggest to provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan Nov 08 '22 at 15:53
1

From your comments, it sounds like you are looking for something like this:

library(ggplot2)

ggplot(interviews_plotting, aes(x = respondent_wall_type, fill = village)) +
  geom_bar(position = position_dodge()) +
  geom_text(stat = 'count', 
            aes(y = stat(count)/2, label = village, group = village), 
            position = position_dodge(width = 1), angle = 90) +
  guides(fill = guide_none())

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"))
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87