根据此页面,以下内容应该产生否图例:
ggplot(df) +
geom_histogram(
aes(
x = vara,
y = 0.5*..density..,
fill = varb
),
color = "black",
binwidth = 10
) +
theme(legend.position = "none") + # this is the line of interest
facet_wrap(~ varb, nrow = 5) +
ylab("Density")
但是,我的填充属性仍然有相同的图例。
使用以下内容,我得不到任何内容:
ggplot(df) +
geom_histogram(
aes(
x = vara,
y = 0.5*..density..,
fill = varb
),
color = "black",
binwidth = 10
) +
guides(fill = FALSE) + # this is the line of interest
facet_wrap(~ varb, nrow = 5) +
ylab("Density")
有什么区别?
编辑:MWE
羞辱我。我遗漏了实际打破图表的代码。但是,我仍然不明白为什么。
这没有用:
library(ggplot2)
df <- data.frame(
vara = seq(5),
varb = c("y","y","n","n","y")
)
prettyplot <- function(p) {
p <- p +
theme_classic() +
theme(text = element_text(
family = "Palatino", size = 14)) +
theme(axis.title.y = element_text(
margin = margin(0,20,0,0))) +
theme(axis.title.x = element_text(
margin = margin(20,0,0,0)))
return(p)
}
p <- ggplot(df) +
geom_histogram(
aes(
x = vara,
y = 0.5*..density..,
fill = varb
),
color = "black",
binwidth = 10
) +
theme(legend.position = "none") + # this is the line of interest
facet_wrap(~ varb, nrow = 5) +
ylab("Density")
prettyplot(p)
答案 0 :(得分:1)
prettyplot
调用theme_classic()
来重置图例。由于prettyplot
在 theme(legend.position="none")
之后被称为,因此图例会被恢复。要仅删除图例一次,您需要在设置theme_classic()
。
theme_classic
恢复图例,因为它基于theme_bw
,它明确地将图例包含在默认位置。这是theme_classic()
:
function (base_size = 12, base_family = "")
{
theme_bw(base_size = base_size, base_family = base_family) %+replace%
theme(panel.border = element_blank(), axis.line = element_line(colour = "black"),
panel.grid.major = element_line(), panel.grid.major.x = element_blank(),
panel.grid.major.y = element_blank(), panel.grid.minor = element_line(),
panel.grid.minor.x = element_blank(), panel.grid.minor.y = element_blank(),
strip.background = element_rect(colour = "black",
size = 0.5), legend.key = element_blank())
}
theme_bw
是44个绘图元素的列表,包括这一个:$ legend.position : chr "right"
,它将图例放在默认位置。