How to move legend box all the way to the left of the plot and align with titles and subtitles in ggplot2?

Viewed 952

I have the following data and picture setup as below

df <- data_frame(group = rep(c("A", "B"), each = 20),
       y = c(sample(1:90, 20, replace = TRUE),
             sample(1:30, 20, replace = TRUE)),
       x = rep(1:20, 2)
       )
ggplot(df, aes(x = x, y = y, color = group))+
   geom_line()+
   labs(x = "", y = "", title = "Comparison", 
     subtitle = "Unit (thousand)", 
     caption = "Source: Kaggle")+
   theme(legend.title = element_blank(), 
     legend.position = "top", 
     legend.justification='left',
     legend.box = "horizontal",
     legend.key = element_blank(),
     legend.box.margin = margin(0,0,0,0,"cm"),
     plot.title.position = "plot",
     plot.caption.position =  "plot",
     plot.caption = element_text(hjust = 0))

and generate the graph here:

enter image description here

How to move legend box all the way to the left of the plot and align with titles and subtitles? Also have to taken into account that there's a margin in legend.key.

Also appreciated it if you can let me know how to reduce the space around legend as well. Thanks!

2 Answers

Here's an approach with specifying legend.position manually, as well as adding \n to the subtitle to create extra space. You'll also need legend.direction = "horizontal" and legend.margin = margin(0):

Note you'll probably have to fiddle with the numbers in legend.position as it will be slightly different for every plotting device.

ggplot(df, aes(x = x, y = y, color = group))+
   geom_line()+
   labs(x = "", y = "", title = "Comparison", 
     subtitle = "Unit (thousand)\n", 
     caption = "Source: Kaggle")+
   theme(legend.title = element_blank(), 
     legend.position = c(-0.065,1.05), 
     legend.justification='left',
     legend.box = "horizontal",
     legend.margin = margin(0),
     legend.key = element_blank(),
     legend.direction = "horizontal",
     plot.title.position = "plot",
     plot.caption.position =  "plot",
     plot.caption = element_text(hjust = 0))

enter image description here

Here is another approach, I thought you want the legend next to the title / subtitle. With the approach of changing plot margins you gain some flexibility. You can use margins to widen the plot area and to position the legend.

library(ggplot2)

df <- data.frame(group = rep(c("A", "B"), each = 20),
                 y = c(sample(1:90, 20, replace = TRUE),
                       sample(1:30, 20, replace = TRUE)),
                 x = rep(1:20, 2)
)
ggplot(df, aes(x = x, y = y, color = group))+
  geom_line()+
  labs(x = "", y = "", title = "Comparison", 
       subtitle = "Unit (thousand)", 
       caption = "Source: Kaggle")+
  theme(legend.title = element_blank(), 
        legend.position = "top", 
        legend.justification='left',
        legend.box = "horizontal",
        legend.key = element_blank(),
        legend.box.margin = margin(-1,0,0,-2,"cm"),
        plot.margin = unit(c(0, 0, 0, 2), "cm"),
        plot.title = element_text(hjust = 0.2),
        plot.subtitle = element_text(hjust = 0.2),
        plot.caption.position =  "plot",
        plot.caption = element_text(hjust = 0)
        )

Created on 2020-06-13 by the reprex package (v0.3.0)

Related