Problem here is a bit obvious I think. I'd like the legend placed (locked) in the top left hand corner of the 'plotting region'. Using c(0.1,0.13) etc is not an option for a number of reasons.
Is there a way to change the reference point for the co-ordinates so they are relative to the plotting region?
mtcars$cyl <- factor(mtcars$cyl, labels=c("four","six","eight")) ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + opts(legend.position = c(0, 1), title="Legend placement makes me sad")
Cheers
position. You can place the legend literally anywhere. To put it around the chart, use the legend. position option and specify top , right , bottom , or left .
In order to draw our legend outside of the plotting area, we can use a combination of the “topright” argument and an additional specification of inset. The “topright” argument specifies that the legend should be in the upper right corner of the graph.
You can use the following syntax to change the legend labels in ggplot2: p + scale_fill_discrete(labels=c('label1', 'label2', 'label3', ...))
Update: opts
has been deprecated. Please use theme
instead, as described in this answer.
Just to expand on kohske's answer, so it's bit more comprehensive for the next person to stumble upon it.
mtcars$cyl <- factor(mtcars$cyl, labels=c("four","six","eight")) library(gridExtra) a <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + opts(legend.justification = c(0, 1), legend.position = c(0, 1), title="Legend is top left") b <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + opts(legend.justification = c(1, 0), legend.position = c(1, 0), title="Legend is bottom right") c <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + opts(legend.justification = c(0, 0), legend.position = c(0, 0), title="Legend is bottom left") d <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + opts(legend.justification = c(1, 1), legend.position = c(1, 1), title="Legend is top right") grid.arrange(a,b,c,d)
I have been looking for similar answer. But found that opts
function is no longer part of ggplot2 package. After searching for some more time, I found that one can use theme
to do similar thing as opts. Therefore editing this thread, so as to minimize others time.
Below is the similar code as written by nzcoops.
mtcars$cyl <- factor(mtcars$cyl, labels=c("four","six","eight")) library(gridExtra) a <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + labs(title = "Legend is top left") + theme(legend.justification = c(0, 1), legend.position = c(0, 1)) b <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + labs(title = "Legend is bottom right") + theme(legend.justification = c(1, 0), legend.position = c(1, 0)) c <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + labs(title = "Legend is bottom left") + theme(legend.justification = c(0, 0), legend.position = c(0, 0)) d <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + labs(title = "Legend is top right") + theme(legend.justification = c(1, 1), legend.position = c(1, 1)) grid.arrange(a,b,c,d)
This code will give exactly similar plot.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With