带有点的R线图在ggplot中突出显示

时间:2018-06-06 13:31:04

标签: r ggplot2

您好我正在尝试在ggplot中重新创建下面的图表,我想知道是否有人可以帮助我。突出显示的红点是被认为是尖峰的地方。

enter image description here

enter image description here

销售年度Roll12 Roll26类型 1 106 70.6 72.5 Spike 2 92 73.9 73.9条件 3 97 77.1 74.7穗 4 75 77.5 75.8正常 5 74 77.6 76.3正常 6 78 78.4 75.8正常 7 76 79.3 75.7正常 8 100 80.8 75.2穗 9 68 79.9 75.1减少 10 73 79.3 74.9减少 11 64 78.6 75.1减少 12 60 77.5 74.2减少

谢谢,

MS

1 个答案:

答案 0 :(得分:1)

这个怎么样

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")

soplot2