r - 在ggplot中突出显示点的R线图
问题描述
嗨,我正在尝试在 ggplot 中重新创建下面的图表,我想知道是否有人可以帮助我。突出显示的红点是它被认为是尖峰的地方。
Month # of Sales Roll12 Roll26 Type 1 106 70.6 72.5 Spike 2 92 73.9 73.9 Condition 3 97 77.1 74.7 Spike 4 75 77.5 75.8 Normal 5 74 77.6 76.3 Normal 6 78 78.4 75.8 Normal 7 76 79.3 75.7 Normal 8 100 80.8 75.2 Spike 9 68 79.9 75.1 减少 10 73 79.3 74.9 减少 11 64 78.6 75.1 减少 12 60 77.5 74.2 减少
谢谢,
多发性硬化症
解决方案
这个怎么样
library("ggplot2")
library("reshape2")
df <- structure(list(Month = 1:12, Sales = c(106L, 92L, 97L,
75L, 74L, 78L, 76L, 100L, 68L, 73L, 64L, 60L), Roll12 = c(70.6,
73.9, 77.1, 77.5, 77.6, 78.4, 79.3, 80.8, 79.9, 79.3, 78.6, 77.5),
Roll26 = c(72.5, 73.9, 74.7, 75.8, 76.3, 75.8, 75.7, 75.2,
75.1, 74.9, 75.1, 74.2), Type = structure(c(4L, 1L, 4L, 3L, 3L,
3L, 3L, 4L, 2L, 2L, 2L, 2L), .Label = c("Condition", "Decrease",
"Normal", "Spike"), class = "factor")), class = "data.frame", row.names = c(NA,
-12L), .Names = c("Month", "Sales", "Roll12", "Roll26",
"Type"))
# need to convert the dataframe to long format for plotting;
# you should change the `variable.name="variable"` and `value.name="value"` args here to whatever you want to call them
df <- reshape2::melt(df, id.vars = c("Month", "Type"))
ggplot(data = df, aes(x = Month, y = value, color = variable)) +
geom_line() +
geom_point(data = df[df[["Type"]] == "Spike" & df[["variable"]] == "Sales", ], color="red")
推荐阅读
- spring-boot - 为什么 Run as Spring Boot App 键盘快捷键在 Spring Tools Suite 中不起作用
- iis - angular universal on iisnode cant find view error
- c++ - 欧拉项目 8 C++ 调试
- spock - 任何更改顺序失败的功能方法的方法都出现在 build/tests/classes/MyTestScript.html
- heroku - 尝试将带有引导模板的静态网站部署到heroku时出现应用程序错误
- javascript - 如何从异步 mongoose 中间件 post hook 中抛出错误
- image - TinyMCE4 `image_list` 外部网址
- sql - 我希望一张桌子继承另一张桌子
- scala - 从可执行 jar 运行 gatling 项目
- node.js - 之后如何从文本文件中 node.js 中的特定行位置删除行