我使用mpgggplot2 包中的数据来测试绘图参数。我发现 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')
)
Run Code Online (Sandbox Code Playgroud)
您需要添加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")
)
Run Code Online (Sandbox Code Playgroud)

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()
)
Run Code Online (Sandbox Code Playgroud)

由reprex 包于 2018 年 11 月 3 日创建(v0.2.1.9000)
| 归档时间: |
|
| 查看次数: |
3945 次 |
| 最近记录: |