linetype and geom_boxplot

163 views
Skip to first unread message

nigol

unread,
Jul 4, 2012, 9:38:19 PM7/4/12
to ggp...@googlegroups.com
I am having a problem with geom_boxplot in ggplot2 0.9.1.
Linetype seems to be a valid aesthetic for this geom, however all lines in the legend are solid.

Reproducible example:  
library(ggplot2)
ggplot(mpg, aes(x=manufacturer, y=cty, linetype=factor(year))) + geom_boxplot()
  
Is there an easy fix to this issue?  
Thanks, 
Dario

Kohske Takahashi

unread,
Jul 5, 2012, 4:04:22 AM7/5/12
to nigol, ggp...@googlegroups.com
Here is the quick fix:

library(ggplot2)
library(proto)

GeomBoxplot2 <- proto(ggplot2:::GeomBoxplot, {
objname <- "boxplot2"
guide_geom <- function(.) "boxplot2"
draw_legend <- function(., data, ...) {
data <- aesdefaults(data, .$default_aes(), list(...))
gp <- with(data, gpar(col=colour, fill=alpha(fill, alpha),
lwd=size * .pt, lty = linetype))
gTree(gp = gp, children = gList(
linesGrob(0.5, c(0.1, 0.25)),
linesGrob(0.5, c(0.75, 0.9)),
rectGrob(height=0.5, width=0.75),
linesGrob(c(0.125, 0.875), 0.5)
))
}
})

geom_boxplot2 <- function (mapping = NULL, data = NULL, stat =
"boxplot", position = "dodge",
outlier.colour = "black", outlier.shape =
16, outlier.size = 2,
notch = FALSE, notchwidth = .5, ...) {
GeomBoxplot2$new(mapping = mapping, data = data, stat = stat,
position = position, outlier.colour =
outlier.colour, outlier.shape = outlier.shape,
outlier.size = outlier.size, notch = notch,
notchwidth = notchwidth, ...)
}

ggplot(mpg, aes(x=manufacturer, y=cty, linetype=factor(year))) + geom_boxplot2()



2012/7/5 nigol <dario....@gmail.com>:
> library(ggplot2)
> ggplot(mpg, aes(x=manufacturer, y=cty, linetype=factor(year))) +
> geom_boxplot()



--
Kohske Takahashi <takahash...@gmail.com>

Assistant Professor,
Research Center for Advanced Science and Technology,
The University of Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

nigol

unread,
Jul 5, 2012, 12:23:26 PM7/5/12
to ggp...@googlegroups.com, nigol
Thank you  Kohske.
I submitted a pull request to Hadley based on the code fix you provided below.
I am new to Github, but I hope to have submitted the pull request correctly.
Thanks,
Dario
Reply all
Reply to author
Forward
0 new messages