gpt4 book ai didi

r - 如何在ggplot中应用legend.key背景透明?

转载 作者:行者123 更新时间:2023-12-03 23:14:49 34 4
gpt4 key购买 nike

我用 mpg ggplot2 包中的数据来测试绘图参数。我发现 lengend.key 背景(右上角,标签车辆模型)颜色可以更改为任何其他颜色,但透明或白色除外。如何将lengend键背景默认灰色更改为透明?

library(ggplot2)
p <- ggplot(data=mpg,mapping=aes(x=cty,y=hwy,colour=class))+
geom_point(aes(size=displ),alpha=0.5)+
stat_smooth(method='loess')+
scale_size_continuous(range=c(4,10))+
facet_wrap(~year,ncol=2)+
labs(x='distance per gallon in city',y='distance per gallon in highway',
title='fuel consumption and model',size='displacement',colour='vehicle model')

p+
theme(
plot.background=element_rect(fill='transparent', colour='transparent'),
panel.background=element_rect(fill='transparent', colour='gray'),
panel.border=element_rect(fill='transparent', colour='black'),
axis.text=element_text(color='black'),
panel.grid.major=element_line(colour='grey',linetype='dashed'),
strip.background=element_rect(fill='transparent', colour='transparent'),
panel.spacing.x=unit(4,'mm'),
legend.box.background=element_rect(fill='blue', colour='transparent'),
legend.key=element_rect(fill='transparent', colour='transparent')
)

enter image description here

最佳答案

您需要添加 se = FALSE或删除 stat_smooth(method = "loess")删除 vehicle model 中的灰色填充背景图例键。

library(ggplot2)

p <- ggplot(data = mpg, mapping = aes(x = cty, y = hwy, colour = class)) +
geom_point(aes(size = displ), alpha = 0.5) +
stat_smooth(method = "loess", se = FALSE) + # Add se = FALSE
scale_size_continuous(range = c(4, 10)) +
facet_wrap(~year, ncol = 2) +
labs(
x = "distance per gallon in city", y = "distance per gallon in highway",
title = "fuel consumption and model", size = "displacement", colour = "vehicle model"
)

p +
theme(
plot.background = element_blank(),
panel.background = element_rect(fill = "transparent", colour = "gray"),
panel.border = element_rect(fill = "transparent", colour = "black"),
axis.text = element_text(color = "black"),
panel.grid.major = element_line(colour = "grey", linetype = "dashed"),
strip.background = element_blank(),
panel.spacing.x = unit(4, "mm"),
legend.box.background = element_rect(fill = "blue", colour = "transparent"),
legend.key = element_rect(fill = "transparent", colour = "transparent")
)



p +
theme(
plot.background = element_rect(fill = "transparent", colour = "transparent"),
panel.background = element_rect(fill = "transparent", colour = "gray"),
panel.border = element_rect(fill = "transparent", colour = "black"),
axis.text = element_text(color = "black"),
panel.grid.major = element_line(colour = "grey", linetype = "dashed"),
strip.background = element_rect(fill = "transparent", colour = "transparent"),
panel.spacing.x = unit(4, "mm"),
legend.background = element_blank(),
legend.box.background = element_blank(),
legend.key = element_blank()
)



创建于 2018-11-03 由 reprex package (v0.2.1.9000)

关于r - 如何在ggplot中应用legend.key背景透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53137793/

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