gpt4 book ai didi

r - 图例上的ggplot2边框但条形图中没有

转载 作者:行者123 更新时间:2023-12-01 11:35:04 27 4
gpt4 key购买 nike

我正在尝试使用 ggplot2 创建条形图,图例颜色有边框,但条形图没有边框。

我尝试添加 scale_fill_manual 并将 'grey32' 映射到市政当局以及 legend.key = element_rect(colour='grey32') 但它们都没有添加图例元素周围的边界。

这是我用这段代码得到的:

ggplot(muns, aes(x=Regional.District, y=Population.2010, fill=Municipality)) +
geom_bar(stat='identity', width=0.6) +
xlab("Regional District") + ylab("Population 2010") +
scale_y_continuous(labels = comma) +
scale_fill_manual(values=fill.vals, breaks=muns$Municipality, labels=muns$lab) +
geom_text(aes(y=bylaw.sums[1], label=c('No Bylaw')), colour = "gray32") +
geom_text(aes(y=bylaw.sums[1]*2+bylaw.sums[2], label=c('Bylaw')), colour = "gray32") +
theme(text=element_text(size=12, family="Open Sans"))

bar_chart_with_no_borders

这是我想要的: bar_chart_with_borders_on_legend_but_not_chart

我试过:

scale_colour_manual(values=greys, breaks=muns$Municipality, labels=muns$lab) +

还有

theme(legend.key = element_rect(colour='grey32'))

谢谢!

最佳答案

试试 + guides(fill = guide_legend(override.aes = list(colour = "black")))

工作示例:

ggplot(iris, aes(x = Species, fill = Species)) +
geom_bar() +
guides(fill = guide_legend(override.aes = list(colour = "black")))

在这种情况下,您似乎确实需要用“u”拼写“colour”。

我通常使用此方法做相反的事情(从填充图例中删除颜色)或覆盖图例的 alpha 设置。

根据您的示例图,您可能还想将 reverse = TRUE 传递给 guide_legend,这会将橙色放在底部,白色放在顶部以匹配您的图.

关于r - 图例上的ggplot2边框但条形图中没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27849624/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com