Take the 2-minute tour ×

I'm using qplot to plot a function and I want to position the legend within the plot. I've used

opts( legend.position = c(0.7,0.7) )

to move the legend where I want it to be.

However there is a white border around the legend and that shows up on the gray background.

For example:

x = c(1:20)
y = c(1:20)

p <- qplot(x,y, color = "blue")

p <- p + scale_colour_identity("Example", breaks=c("blue"), labels=c("dots"))

p <- p + opts(legend.position = c(0.6, 0.4))


I would like to know how to remove this border from the legend. Thank you.

share|improve this question
You only need to use c() around multiple values so 1:20, "blue" and "dots" dont need the use of c() combine function. Thought I'd mention in case that saves some typing or helps otherwise. –  Jay Feb 12 '10 at 4:15
add comment

1 Answer

up vote 5 down vote accepted

This will get rid of your border:

p + opts(legend.background = theme_rect(col = 0))

other options in addition to col (which applies to the border) are fill (background) and size (which is the border size).

Hope that helps!

All the best,


share|improve this answer
Thanks Jay - that worked perfectly. When you mentioned fill(background) is that used similarly? e.g. opts(legend.fill = "white")? I tried combinations of this without success. –  celenius Feb 12 '10 at 14:03
p + opts(legend.background = theme_rect(col = "red", size = 2, fill = "blue")) –  Jay Feb 12 '10 at 14:09
This will give you a slightly thicker red border and a blue backgound. Does that help? Let me know if that works –  Jay Feb 12 '10 at 14:11
Yes that does help, thank you. Now I understand how to use it. –  celenius Feb 12 '10 at 14:30
add comment

Your Answer


