我试图仅突出显示 ggplot2 中组合线 + 点图上的某些点。
这里有一点背景。在这个数据集中,每当一个值超出某个范围时,它就会被标记为超出规范。在“in_spec”列中,如果一个值为 0,则它超出了指定范围。这是数据:
dat <- structure(list(Date = structure(c(1592784000, 1592784000, 1592784000,
1592784000, 1592870400, 1592870400, 1592870400, 1592870400, 1593388800,
1593388800, 1593388800, 1593388800, 1593475200, 1593475200, 1593475200,
1593475200, 1593561600, 1593561600, 1593561600, 1593561600, 1592956800,
1593043200, 1593129600, 1593648000, 1594166400, 1594684800, 1594771200,
1594857600, 1594944000, 1594252800, 1594339200), tzone = "UTC", class =
c("POSIXct", "POSIXt")),
variable = c("var1", "var1", "var1", "var1", "var1", "var1", "var1",
"var1", "var1", "var1", "var1", "var1", "var1", "var1",
"var1", "var1", "var1", "var1", "var1", "var1", "var1",
"var1", "var1", "var1", "var1", "var1", "var1", "var1",
"var1", "var1", "var1"),
reading = c(100.1, 100.1, 100.1, 100.1, 100.09, 100.09, 100.09, 100.09, 100.14,
100.14, 100.14, 100.14, 100.13, 100.13, 100.13, 100.13, 100.14,
100.14, 100.14, 100.14, 100.08, 100.05, 90.53, 100.14, 100.14,
90.3, 100.15, 100.14, 100.13, NA, NA),
in_spec = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 0, 1, 1, 1, NA, NA)),
row.names = c(NA, -31L), class = c("tbl_df", "tbl", "data.frame"))
绘制趋势很容易,并且使用 gghighlight,我已经能够突出显示我所追求的值。这是代码和输出:
p <- ggplot(dat, aes(x = Date, y = reading)) +
#date along the x axis, reading as the y
geom_point() +
#first plot the points
gghighlight::gghighlight(in_spec == 0) +
#highlight points that are flagged with 0
geom_line()
#add the line connecting the points
p
这几乎是正确的,只是它连接了“不合规格”的两个点,即使它们实际上并没有在时间上彼此相邻。
如何仅突出显示“超出规格”的点,但留下连接所有其他点的线?最终目标将是相同的情节,但只有下面两个突出显示的点,它们之间没有线。
我尝试重新排列 geom_line 和 geom_point 调用的顺序,并在不同的位置进行 gghighlight 调用。