So my problem is pretty straightforward, but somehow I haven’t found cases that are similar to mine. I have a legend that takes two rows but I want it to take one.
Here is a reproducible example :
df <- expand.grid(Individual = LETTERS[1:4], Category = paste0("Long name for a category", 1:6))
df <- df %>%
group_by(Individual) %>%
mutate(Share = runif(n(), min = 0, max = 100)) %>%
group_by(Individual) %>%
mutate(Share = Share / sum(Share) * 100)
(Graph <- df %>%
ggplot(aes(x = Share , y = Individual, fill = Category)) +
geom_bar(stat = "identity", color = "black") +
guides(fill = guide_legend(nwrow = 1)) +
theme(plot.title = element_text(size = 15, hjust = 0.5),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
legend.direction = "horizontal",
legend.position = "bottom"))
Here is the result of the code, and as you can see, despite using guides and nrow = 1, my legend is still in two rows. I used shorter names for my variables to see if this was the problem, but it’s not.
Any ideas on how to make it work ?
1