I want to place a legend inside a ggplot2 graph. See for example the code
ggplot(iris, aes(Sepal.Length, Petal.Width, shape = Species, colour = Species)) +
geom_point() +
theme(legend.position.inside = c(0.8, 0.2))
I want the resulting legend to be in the lower right hand corner of the graph, i.e., superimposed on the grey background, but legend.position.inside is always placing the legend outside the graph
The help docs for ?theme say legend.position.inside is "A numeric vector of length two setting the placement of legends that have the "inside" position."
So for that parameter to do anything you also need legend.position = "inside"
ggplot(iris, aes(Sepal.Length, Petal.Width, shape = Species, colour = Species)) +
geom_point() +
theme(legend.position = "inside",
legend.position.inside = c(0.8, 0.2))
Or you can use theme(legend.position = c(0.8, 0.2))
, but that option was deprecated in ggplot2 3.5.0 and that will generate a warning suggesting us to use the option above.