[R] Patch for legend.position={left,top,bottom} in ggplot2

From: Karsten Loesing <karsten.loesing_at_gmx.net>
Date: Mon, 07 Jun 2010 15:39:46 +0200


Hi Hadley and everyone,

here's a patch for ggplot2 that fixes the behavior of opts(legend.position={left,top,bottom}). If you try the following code in an unmodified ggplot2

options(warn = -1)
suppressPackageStartupMessages(library("ggplot2")) data <- data.frame(

    x = c(1, 2, 3, 4, 5, 6),
    y = c(2, 3, 4, 3, 4, 5),
    colour = c(TRUE, TRUE, TRUE, FALSE, FALSE, FALSE)) ggplot(data, aes(x = x, y = y, colour = colour)) +

    geom_line() + opts(title = "title", legend.position = "right") ggplot(data, aes(x = x, y = y, colour = colour)) +

    geom_line() + opts(title = "title", legend.position = "left") ggplot(data, aes(x = x, y = y, colour = colour)) +

    geom_line() + opts(title = "title", legend.position = "top") ggplot(data, aes(x = x, y = y, colour = colour)) +

    geom_line() + opts(title = "title", legend.position = "bottom")

you'll see that plots 2 to 4 are broken.

I think I located the bug in surround_viewports() where the graphical elements are placed into the grid. If we increment all rows and columns of the graphical elements for positions "left", "top", and "bottom" by 1, those graphs look sane again. I assume that a new first row and column were added at some point in the development, but only the parameters for the default position "right" were adjusted. Here's the patch:

Best,
--Karsten



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 07 Jun 2010 - 13:42:41 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 10 Jun 2010 - 08:00:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive