【问题标题】:Define and specify legend quantiles scatter plot R定义和指定图例分位数散点图 R
【发布时间】: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


【解决方案1】:

我找到了自己问题的答案。

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 = "25%"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.50)), geom = "hline", aes(yintercept = ..y.., colour = "50%"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.75)), geom = "hline", aes(yintercept = ..y.., colour = "75%"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.90)), geom = "hline", aes(yintercept = ..y.., colour = "90%"), size = 1) +
stat_summary(fun.y = "mean", geom = "hline", aes(yintercept = ..y.., colour = "mean"), size = 1.5) +
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")) +
scale_colour_manual(values = c("red", "blue", "gold", "green", "black"), name = "Percentiles"))
jitter

此外,很快,“只需使用(某物),每个人都会得到它”的想法作为建议是没有帮助的,并且对最终目标受众的假设过多。我第一次发布问题并将其作为回复。我出于特定的原因问了一个特定的问题。

【讨论】:

    猜你喜欢
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多