我将ggplot箱线图与平均平均值的连接线组合在一起。
如何创建图例,使人们知道蓝色圆圈点代表每个箱线图的平均数?
library(ggplot2)
library(scales)
options(scipen=11)
ggplot(Price, aes(x=Price$stage_code, y=Price$Realvalue)) +
scale_y_continuous(labels = comma) +
geom_boxplot(notch=FALSE, outlier.shape=NA, fill="red", alpha=0.2) +
coord_cartesian(ylim=c(0,1000000000)) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
ggtitle("Average True Value of Listed Mining Companies\nThroughout Mine Development Stages") +
xlab("Project Development Stages") +
ylab("Number of Diluted Stocks x Closing Stock Price") +
stat_summary(fun.y=mean, geom="line", linetype="dotted",
size=1.4, color = "Blue",alpha=0.6, aes(group=1)) +
stat_summary(fun.y=mean, geom="point", alpha=1, color="darkblue",
size=3.2, shape = 21, fill = "lightblue", stroke = 2)
答案 0 :(得分:1)
ggplot2仅为基于变量分配的颜色添加图例。
编辑:我从this answer意识到可以手动添加图例。这是一种更好的方法。
只需在aes
中映射颜色,然后使用scale_color_manual
添加标题并指定颜色:
stat_summary(aes(color="Legend"),fun.y=mean, geom="point", alpha=1,
size=3.2, shape = 21, fill = "lightblue", stroke = 2) +
scale_colour_manual("Legend title", values="darkblue")