我在下面有例如散点图(抖动)图的数据和语法
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
我遇到了这样的问题/答案,但是我无法实现它,当我在es设置中包含颜色时,它是行不通的。
编辑-一位成员建议我将颜色添加到es规范中...这里是包含颜色和尺寸的同一张图。
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
因此...感谢您的帮助。谢谢!
答案 0 :(得分:0)
我找到了自己问题的答案。
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
很快,“合理使用(某物),每个人都能得到”的想法作为建议并没有帮助,并且对最终目标受众的假设过多。第一次,我发布了一个问题并得到了答复。我出于特定原因问了一个具体问题。