【发布时间】:2019-05-18 00:39:06
【问题描述】:
下面有散点图(抖动)的数据和语法
eg_data <- data.frame(
period = c(sample( c("1 + 2"), 1000, replace = TRUE)),
max_sales = c(sample( c(1,2,3,4,5,6,7,8,9,10), 1000, replace = TRUE, prob =
c(.20, .10, .15, .20, .15, .10, .05, .02, .02, .01))) )
jitter <- (
(ggplot(data = eg_data, aes(x=period, y=max_sales)) +
geom_jitter(stat = "identity", width = .15, color = "blue", alpha = .4)) +
scale_y_continuous(breaks= seq(0,12, by=1)) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.25)), geom = "hline", aes(yintercept = ..y..), colour = "red", size = 1) +
stat_summary(fun.y = "mean", geom = "hline", aes(yintercept = ..y..), colour = "gold", size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.50)), geom = "hline", aes(yintercept = ..y..), colour = "blue", size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.75)), geom = "hline", aes(yintercept = ..y..), colour = "black", size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.90)), geom = "hline", aes(yintercept = ..y..), colour = "green", size = 1) +
ggtitle("Max Sales x Period 1 and 2") + xlab("Period") + ylab("Sales") +
theme(plot.title = element_text(color = "black", size = 14, face = "bold", hjust = 0.5),
axis.title.x = element_text(color = "black", size = 12, face = "bold"),
axis.title.y = element_text(color = "black", size = 12, face = "bold")) +
labs(fill = "Period") )
jitter
我找不到有关如何为该图中的水平分位数/平均线定义图例的文档。
How to add legend to ggplot manually? - R
我遇到了这个 SO 问题/答案,但我无法实现它,当我在 aes 设置中包含颜色时,它不起作用。
编辑 - 一位成员建议我在 aes 规范中添加颜色...这是包含颜色和大小的相同图表。
jitter2 <- (
(ggplot(data = eg_data, aes(x=period, y=max_sales)) +
geom_jitter(stat = "identity", width = .15, color = "blue", alpha = .4)) +
scale_y_continuous(breaks= seq(0,12, by=1)) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.25)), geom = "hline", aes(yintercept = ..y.., colour = "red"), size = 1) +
stat_summary(fun.y = "mean", geom = "hline", aes(yintercept = ..y.., colour = "gold"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.50)), geom = "hline", aes(yintercept = ..y.., colour = "blue"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.75)), geom = "hline", aes(yintercept = ..y.., colour = "black"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.90)), geom = "hline", aes(yintercept = ..y.., colour = "green"), size = 1) +
ggtitle("Max Sales x Period 1 and 2") + xlab("Period") + ylab("Sales") +
theme(plot.title = element_text(color = "black", size = 14, face = "bold", hjust = 0.5),
axis.title.x = element_text(color = "black", size = 12, face = "bold"),
axis.title.y = element_text(color = "black", size = 12, face = "bold")) +
labs(fill = "Period") )
jitter2
所以...任何帮助表示赞赏。谢谢!
【问题讨论】:
-
你为什么不用箱线图?它显示相同的分位数信息,每个人都可以理解。
-
"我遇到了这个 SO 问题/答案" -- 好像你忘记了链接
-
为了获得图例,您必须为美学指定一些东西,例如颜色
-
@PoGibas - 每个人都绝对无法理解箱线图。在十年的分析工作中,我的经验是,公众根本无法理解它们。但他们确实有线路,这就是我寻求帮助的原因。
-
@camile,我编辑了问题并发布了链接,对不起。我还编辑了问题,在 aes 参数中添加了颜色和大小的第二个抖动,以说明为什么这对我不起作用。
标签: r ggplot2 legend scatter-plot