[英]Changing fill of legend shape in ggplot
I have a rather complicated plot that shows three different variables in my data - see the example plot below. 我有一个相当复杂的图,在我的数据中显示了三个不同的变量-请参见下面的示例图。 I've removed one of the legends and am trying to show the
consonant
colours more clearly by filling in the squares. 我已删除其中一个图例,并试图通过填充正方形来更清楚地显示
consonant
颜色。 I've tried using guide_legend(override.aes())
but I can't find a way to adjust the colours on this. 我试过使用
guide_legend(override.aes())
但是找不到调整颜色的方法。 Is there a way to do it? 有办法吗?
The sample data is: 样本数据为:
exampledata <- tribble(~subject, ~group, ~time, ~consonant, ~type, ~n,
"1", "A", 1, "p", F, 10,
"1", "A", 1, "t", T, 12,
"1", "A", 1, "k", T, 50,
"2", "A", 1, "p", T, 0,
"2", "A", 1, "t", T, 45,
"2", "A", 1, "k", F, 23,
"2", "A", 2, "p", F, 2,
"2", "A", 2, "t", T, 34,
"2", "A", 2, "k", T, 56,
"3", "B", 1, "p", F, 12,
"3", "B", 1, "t", T, 13,
"3", "B", 1, "k", F, 50,
"4", "A", 1, "p", T, 10,
"4", "A", 1, "t", F, 12,
"4", "A", 1, "k", T, 50,
"5", "B", 1, "p", T, 0,
"5", "B", 1, "t", T, 24,
"5", "B", 1, "k", F, 3,
"5", "B", 2, "p", F, 23,
"5", "B", 2, "t", F, 12,
"5", "B", 2, "k", T, 7,
"6", "A", 1, "p", F, 52,
"6", "A", 1, "t", F, 12,
"6", "A", 1, "k", T, 64
)
And the ggplot
code that I have so far: 以及到目前为止的
ggplot
代码:
exampleplot <- ggplot(exampledata, aes(x = time, y=n,
fill=type, colour = consonant)) +
geom_col(lwd = 1, aes(linetype = group)) +
scale_linetype_manual(values = c("A" = "solid",
"B" = "twodash"), guide = F) +
facet_grid(~subject, scales = "free_x", switch = "x") +
scale_fill_manual(values=c("gray97", "gray77"),
labels = c("Type 1", "Type 2")) +
theme_bw(base_size = 18) +
theme(strip.text.x = element_text(angle=75),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
panel.border =element_blank(),
strip.background = element_blank())
plot(exampleplot)
exampleplot <- ggplot(exampledata, aes(x = time, y=n,
alpha=type, fill = consonant, color = consonant)) +
geom_col(lwd = 1, aes(linetype = group)) +
scale_linetype_manual(values = c("A" = "solid",
"B" = "twodash"), guide = F) +
facet_grid(~subject, scales = "free_x", switch = "x") +
scale_alpha_discrete(range=c(0.4,0.8),
labels = c("Type 1", "Type 2")) +
theme_bw(base_size = 18) +
theme(strip.text.x = element_text(angle=75),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
panel.border =element_blank(),
strip.background = element_blank())
plot(exampleplot)
I added a fill
in the aes
and alpha
to keep the differences between Type
. 我在
aes
和alpha
添加了fill
,以保持Type
之间的差异。 Is this the output you want with filled squared? 您要用实心平方输出吗?
Try this: 尝试这个:
ggplot(exampledata, aes(x = time, y=n,
fill=type, colour = consonant)) +
geom_col(lwd = 1, aes(linetype = group)) +
guides(colour = guide_legend(override.aes = list(fill = c("#F8766D", "#00BA38", "#619CFF")))) +
scale_linetype_manual(values = c("A" = "solid",
"B" = "twodash"), guide = F) +
facet_grid(~subject, scales = "free_x", switch = "x") +
scale_fill_manual(values=c("gray97", "gray77"),
labels = c("Type 1", "Type 2")) +
theme_bw(base_size = 18) +
theme(strip.text.x = element_text(angle=75),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
panel.border =element_blank(),
strip.background = element_blank())
If have added this line: 如果已添加此行:
guides(colour = guide_legend(override.aes = list(fill = c("#F8766D", "#00BA38", "#619CFF"))))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.