Legend positioning on ggplot

I would like to check if someone knows how to format legend so that text values in legend are not presented in two lines bur rather on one single line and attachment.

Bellow is the code

countries_plot <- data_for_countries%>%
dplyr::group_by(Country_of_departure,sorted_months)%>%
dplyr::summarise(monthly_total = sum(Total))%>%
filter(monthly_total > 0)%>%
ggplot(aes(sorted_months, monthly_total))+
geom_col(aes(fill = Country_of_departure),position = "fill")+
scale_y_continuous(labels = scales::percent)+
theme(legend.title = element_blank(),
legend.position = "top",
legend.box = "horizontal",
legend.box.just = "left")

countries_plot

I couldn't get the legend to wrap automatically for some reason but there is an nrow argument available for legends.

library(ggplot2)
DF <- data.frame(Name = rep(LETTERS[1:4], 4),
                 Sub = rep(c("Long Name and Even More More 1", 
                             "Long Name and Even More More 2", 
                             "Long Name and Even More 3", 
                             "Long Nmae and Even More 4"), each = 4),
                 Value = runif(16, 1, 20))
Plt <- ggplot(DF, aes(Name, Value, fill = Sub)) + geom_col() + 
  theme(legend.title = element_blank(),
        legend.position = "top",
        legend.box = "horizontal",
        legend.box.just = "left")
Plt


Plt + guides(fill = guide_legend(nrow = 2))

Created on 2020-01-03 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.