Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Legend placement, ggplot, relative to plotting region

Tags:

r

ggplot2

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") 

enter image description here

Cheers

like image 201
nzcoops Avatar asked May 25 '12 01:05

nzcoops


People also ask

How do I change the position of my legend in R?

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 .

How do you make a legend outside the plot in R?

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.

How do I change the legend value in ggplot2?

You can use the following syntax to change the legend labels in ggplot2: p + scale_fill_discrete(labels=c('label1', 'label2', 'label3', ...))


2 Answers

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) 

enter image description here

like image 197
nzcoops Avatar answered Oct 02 '22 10:10

nzcoops


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.

like image 38
Kumar Manglam Avatar answered Oct 02 '22 11:10

Kumar Manglam