如何更改ggplot2中的图例几何 [英] How can I change the legend geometry in ggplot2
本文介绍了如何更改ggplot2中的图例几何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,我画了这个箱线图:
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
问题是我该如何用行替换图例中的那些小框(就像我将使用 graphics
一样)
Question is how can I replace those little boxes in the legend by lines (like I would have using graphics
)
推荐答案
最简单的选择可能是使线条不可见
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
(如果您在同一会话中需要原始版本,最好先克隆GeomBoxplot).
(probably better to clone GeomBoxplot first if you need the original in the same session).
这篇关于如何更改ggplot2中的图例几何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文