如何在 geom_count ggplot 中为道具图例下方的 stat_summary 点添加第二个图例?
How to add a second legend for stat_summary point below prop legend in geom_count ggplot?
我有一个 geom_count 图并使用 stat_summary 添加 Y 轴(重要性)的平均值作为点。我希望点的形状(在我的例子中是红色矩形)成为图例的一部分,以便很明显这些是方法。如何让手段符号显示在道具图例下方的图例中?
#example data:
df <- data.frame(x = c("A", "B", "C", "D", "E"),
y = c(1, 2, 3, 4, 5, 5, 4, 3, 2, 1))
#plot
ggplot(df, aes(x = x, y = y)) +
geom_count(aes(size= after_stat(prop), group=x)) +
theme_bw() +
scale_size_area(max_size = 8) +
stat_summary(
geom = "point",
fun = "mean",
col = "black",
size = 6,
shape = 23,
fill = "red",
show.legend = F) +
xlab("") +
ylab("Importance") +
scale_y_continuous(labels=c("1" = "Not at all", "2" = "Little ", "3" = "Moderate", "4" = "Quite" , "5" = "Very")) +
theme(axis.text.x = element_text(angle = 30, vjust = 1, hjust=1),
axis.text.y = element_text(angle =30))
改为show.legend = T
并不能解决问题
我已经尝试了 but could not get it to work in my graph. Using the suggestions of this previous SO question中建议的解决方案我添加了代码
geom_point(aes(shape = "mean"), alpha = 0)+
guides(shape=guide_legend(title=NULL, override.aes = list(alpha = 1)))
... 这帮助我在图例中添加了一个方法点,但我无法添加正确的形状 (see this plot here)
使用scale_shape_manual
:
df <- data.frame(x = c("A", "B", "C", "D", "E"),
y = c(1, 2, 3, 4, 5, 5, 4, 3, 2, 1))
# Plot
ggplot(df, aes(x = x, y = y)) +
geom_count(aes(size= after_stat(prop), group=x)) +
theme_bw() +
scale_size_area(max_size = 8) +
xlab("") +
ylab("Importance") +
scale_y_continuous(labels=c("1" = "Not at all", "2" = "Little ", "3" = "Moderate", "4" = "Quite" , "5" = "Very")) +
theme(axis.text.x = element_text(angle = 30, vjust = 1, hjust=1),
axis.text.y = element_text(angle =30)) +
# Stat_summary and legend
stat_summary(fun = mean, aes(shape = "Mean"), geom = 'point', col = "black", fill = "red", size = 6) +
scale_shape_manual(values=c("Mean" = 23))
我有一个 geom_count 图并使用 stat_summary 添加 Y 轴(重要性)的平均值作为点。我希望点的形状(在我的例子中是红色矩形)成为图例的一部分,以便很明显这些是方法。如何让手段符号显示在道具图例下方的图例中?
#example data:
df <- data.frame(x = c("A", "B", "C", "D", "E"),
y = c(1, 2, 3, 4, 5, 5, 4, 3, 2, 1))
#plot
ggplot(df, aes(x = x, y = y)) +
geom_count(aes(size= after_stat(prop), group=x)) +
theme_bw() +
scale_size_area(max_size = 8) +
stat_summary(
geom = "point",
fun = "mean",
col = "black",
size = 6,
shape = 23,
fill = "red",
show.legend = F) +
xlab("") +
ylab("Importance") +
scale_y_continuous(labels=c("1" = "Not at all", "2" = "Little ", "3" = "Moderate", "4" = "Quite" , "5" = "Very")) +
theme(axis.text.x = element_text(angle = 30, vjust = 1, hjust=1),
axis.text.y = element_text(angle =30))
改为show.legend = T
并不能解决问题
我已经尝试了
geom_point(aes(shape = "mean"), alpha = 0)+
guides(shape=guide_legend(title=NULL, override.aes = list(alpha = 1)))
... 这帮助我在图例中添加了一个方法点,但我无法添加正确的形状 (see this plot here)
使用scale_shape_manual
:
df <- data.frame(x = c("A", "B", "C", "D", "E"),
y = c(1, 2, 3, 4, 5, 5, 4, 3, 2, 1))
# Plot
ggplot(df, aes(x = x, y = y)) +
geom_count(aes(size= after_stat(prop), group=x)) +
theme_bw() +
scale_size_area(max_size = 8) +
xlab("") +
ylab("Importance") +
scale_y_continuous(labels=c("1" = "Not at all", "2" = "Little ", "3" = "Moderate", "4" = "Quite" , "5" = "Very")) +
theme(axis.text.x = element_text(angle = 30, vjust = 1, hjust=1),
axis.text.y = element_text(angle =30)) +
# Stat_summary and legend
stat_summary(fun = mean, aes(shape = "Mean"), geom = 'point', col = "black", fill = "red", size = 6) +
scale_shape_manual(values=c("Mean" = 23))