Multiple Legend Justification

I am trying to justify several legends in ggplot, but without any real success. If the legend is displayed correctly, outside the plot area (gray area). However, when you show the legends inside the plot area, the legends are centered (and I would like them to be left-aligned). I tried to execute this thread , but it still does not work properly.

My example:

library(ggplot2) ggplot(mtcars, aes(wt, mpg)) + geom_point(aes(colour = factor(cyl), size = qsec)) + geom_point(aes(colour = factor(cyl), size = qsec)) + theme(legend.justification = c(1,0), legend.position = c(1,0), legend.margin = unit(0,"lines"), legend.box = "vertical", legend.key.size = unit(1,"lines"), legend.text.align = 0, legend.title.align = 0) 
+5
source share
2 answers

We need to add legend.box.just = "left" to the existing theme() .

 ggplot(mtcars, aes(wt, mpg)) + geom_point(aes(colour = factor(cyl), size = qsec)) + geom_point(aes(colour = factor(cyl), size = qsec)) + theme(legend.box.just = "left", legend.justification = c(1,0), legend.position = c(1,0), legend.margin = unit(0,"lines"), legend.box = "vertical", legend.key.size = unit(1,"lines"), legend.text.align = 0, legend.title.align = 0) 

enter image description here

+5
source

You can try the following:

 library(ggplot2) data("mtcars") g <- ggplot(mtcars, aes(wt, mpg)) g <- g + geom_point(aes(colour = factor(cyl), size = qsec)) g <- g + geom_point(aes(colour = factor(cyl), size = qsec)) g <- g + theme(legend.justification=c(0,0), legend.position=c(0,0)) 

For other positions, you can try from this documentation http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/

enter image description here

You will probably find a more detailed explanation in the link above.

+1
source

Source: https://habr.com/ru/post/1247013/


All Articles