问题
Hello say I plot this boxplot:
library(ggplot2)
DT <- data.frame(
y = runif(400, max = 2),
grp = sample(c('M', 'F'),size = 400, replace = T),
x = rep(as.Date(1:10,origin='2011-01-01'), each = 40)
)
p <- ggplot(DT) + geom_boxplot() + aes(x = x, y = y, group=interaction(x,grp), fill=grp)
p
Question is how can I replace those little boxes in the legend by lines (like I would have using graphics
)
回答1:
easiest option might be to make the lines invisible,
p + guides(fill = guide_legend(override.aes = list(col=NA)))
alternatively, you could overwrite the key for the boxplot geom,
my_key = function (data, params, size)
{
grid::rectGrob(height=grid::unit(2,"mm"),
gp = grid::gpar(col = NA,
fill = scales::alpha(data$fill, data$alpha),
lty = data$linetype))
}
GeomBoxplot$draw_key <- my_key
p
(probably better to clone GeomBoxplot first if you need the original in the same session).
来源:https://stackoverflow.com/questions/35529516/how-can-i-change-the-legend-geometry-in-ggplot2