在 geom_bar 和 geom_smooth 中使用 aes(fill=kind) 时如何避免图例键中的框?
How to avoid the box in legend key when using aes(fill=kind) in both geom_bar and geom_smooth?
在 geom_bar 和 geom_smooth 中使用 aes(fill=kind) 时,我无法得到清晰的图例和相同的情节,如下所示:
这是脚本:
#!/usr/bin/env Rscript
## load
library(ggplot2)
library(dplyr)
mma <- data.frame(kind=c(rep('A',100), rep('BB', 100), rep('CC',100)),
group=c(rep(seq(0.1, 10, by=0.1),3)),
frac=runif(n=300, min=0, max=1))
### pic
wd <- 0.05
posa <- position_dodge(width=wd)
ppa <- ggplot(data=mma, aes(x=group, y=frac, group=kind))+
geom_bar(aes(fill=kind), stat='identity', position=posa, width=wd)+
geom_smooth(aes(fill=kind, color=kind), method="loess", span=0.15, size=0.5)+
labs(x="Time", y="Populations")+
## default mina.png
## guides(fill=guide_legend(ncol=3, byrow=TRUE), color="none")+ # minb.png
## guides(color=guide_legend(ncol=3, byrow=TRUE), fill="none")+ # minc.png
theme(legend.position=c(0.66, 0.92),
legend.key.size=unit(0.56, 'lines'))
### output
pf <- c(3, 7)
ggsave(file = "./mina.png", height = pf[1], width = pf[2])
默认设置输出(mina.png
)
禁用颜色 (minb.png
)
禁用填充 (minc.png
)
不知道为什么minb.png
的图例里有蓝线和框(好像是出自geom_smooth
)。
我知道 minc.png
中的灰色 rect/box 可以通过使用 geom_smooth
中的 alpha=1
来省略。但是,它也会擦除黄土线的跨度区域。
您可以在 geom_smooth
中添加 show.legend = FALSE
:
library(tidyverse)
mma <- data.frame(kind=c(rep('A',100), rep('BB', 100), rep('CC',100)),
group=c(rep(seq(0.1, 10, by=0.1),3)),
frac=runif(n=300, min=0, max=1))
### pic
wd <- 0.05
posa <- position_dodge(width=wd)
ggplot(data=mma, aes(x=group, y=frac, group=kind))+
geom_bar(aes(fill=kind), stat='identity', position=posa, width=wd)+
geom_smooth(aes(fill=kind, color=kind), method="loess", span=0.15, size=0.5, show.legend = FALSE)+
labs(x="Time", y="Populations")+
## default mina.png
guides(fill=guide_legend(ncol=3, byrow=TRUE), color="none")+ # minb.png
## guides(color=guide_legend(ncol=3, byrow=TRUE), fill="none")+ # minc.png
theme(legend.position=c(0.66, 0.92),
legend.key.size=unit(0.56, 'lines'))
#> `geom_smooth()` using formula 'y ~ x'
由 reprex package (v2.0.1)
于 2022-04-28 创建
如果我理解正确的话,您想要一个干净的填充图例和颜色图例 side-by-side,就像问题顶部的图片一样。在这种情况下,您可以使用 guides
:
来实现效果
ggplot(mma, aes(group, frac, fill = kind, color = kind)) +
geom_col(position = posa, width = wd) +
geom_smooth(method = "loess", span = 0.15, size = 0.5) +
labs(x = "Time", y = "Populations", color = 'kind ') +
guides(fill = guide_legend(override.aes = list(colour = NA, alpha = 1)),
colour = guide_legend(override.aes = list(fill = 'white', alpha = 1))) +
theme(legend.position = c(0.66, 0.92),
legend.key.size = unit(0.56, 'lines'),
legend.direction = 'horizontal',
legend.box = 'horizontal')
在 geom_bar 和 geom_smooth 中使用 aes(fill=kind) 时,我无法得到清晰的图例和相同的情节,如下所示:
这是脚本:
#!/usr/bin/env Rscript
## load
library(ggplot2)
library(dplyr)
mma <- data.frame(kind=c(rep('A',100), rep('BB', 100), rep('CC',100)),
group=c(rep(seq(0.1, 10, by=0.1),3)),
frac=runif(n=300, min=0, max=1))
### pic
wd <- 0.05
posa <- position_dodge(width=wd)
ppa <- ggplot(data=mma, aes(x=group, y=frac, group=kind))+
geom_bar(aes(fill=kind), stat='identity', position=posa, width=wd)+
geom_smooth(aes(fill=kind, color=kind), method="loess", span=0.15, size=0.5)+
labs(x="Time", y="Populations")+
## default mina.png
## guides(fill=guide_legend(ncol=3, byrow=TRUE), color="none")+ # minb.png
## guides(color=guide_legend(ncol=3, byrow=TRUE), fill="none")+ # minc.png
theme(legend.position=c(0.66, 0.92),
legend.key.size=unit(0.56, 'lines'))
### output
pf <- c(3, 7)
ggsave(file = "./mina.png", height = pf[1], width = pf[2])
默认设置输出(
mina.png
)禁用颜色 (
minb.png
)禁用填充 (
minc.png
)
不知道为什么minb.png
的图例里有蓝线和框(好像是出自geom_smooth
)。
我知道 minc.png
中的灰色 rect/box 可以通过使用 geom_smooth
中的 alpha=1
来省略。但是,它也会擦除黄土线的跨度区域。
您可以在 geom_smooth
中添加 show.legend = FALSE
:
library(tidyverse)
mma <- data.frame(kind=c(rep('A',100), rep('BB', 100), rep('CC',100)),
group=c(rep(seq(0.1, 10, by=0.1),3)),
frac=runif(n=300, min=0, max=1))
### pic
wd <- 0.05
posa <- position_dodge(width=wd)
ggplot(data=mma, aes(x=group, y=frac, group=kind))+
geom_bar(aes(fill=kind), stat='identity', position=posa, width=wd)+
geom_smooth(aes(fill=kind, color=kind), method="loess", span=0.15, size=0.5, show.legend = FALSE)+
labs(x="Time", y="Populations")+
## default mina.png
guides(fill=guide_legend(ncol=3, byrow=TRUE), color="none")+ # minb.png
## guides(color=guide_legend(ncol=3, byrow=TRUE), fill="none")+ # minc.png
theme(legend.position=c(0.66, 0.92),
legend.key.size=unit(0.56, 'lines'))
#> `geom_smooth()` using formula 'y ~ x'
由 reprex package (v2.0.1)
于 2022-04-28 创建如果我理解正确的话,您想要一个干净的填充图例和颜色图例 side-by-side,就像问题顶部的图片一样。在这种情况下,您可以使用 guides
:
ggplot(mma, aes(group, frac, fill = kind, color = kind)) +
geom_col(position = posa, width = wd) +
geom_smooth(method = "loess", span = 0.15, size = 0.5) +
labs(x = "Time", y = "Populations", color = 'kind ') +
guides(fill = guide_legend(override.aes = list(colour = NA, alpha = 1)),
colour = guide_legend(override.aes = list(fill = 'white', alpha = 1))) +
theme(legend.position = c(0.66, 0.92),
legend.key.size = unit(0.56, 'lines'),
legend.direction = 'horizontal',
legend.box = 'horizontal')