1

我试图仅突出显示 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 调用。

4

2 回答 2

3

一种简单的方法是geom_line()根据 where 对数据进行子集化in_spec==1,然后突出显示 where 的数据in_spec==0

ggplot(dat, aes(x = Date, y = reading)) +
  geom_point() +
  geom_line(data=subset(dat, in_spec==1)) +
  gghighlight::gghighlight(in_spec == 0)

在此处输入图像描述

于 2020-07-20T21:18:50.363 回答
0

我找到了一个解决方案,但它不涉及gghighlight。添加点时设置颜色会将点绘制为不同的颜色。然而,重要的是要注意,分组列不能是连续变量,即因子或布尔值。

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

dat <- dat %>%
  mutate(in_spec = as.factor(in_spec))
#convert the in_spec column to a factor so the scale_color_manual will work below

p <- ggplot(dat, aes(x = Date, y = reading)) +
    geom_line(color = "grey") +
    #add a line connecting the readings, color it grey
    geom_point(aes(color = in_spec), size = 1) +
    #add points for each of the readings, grouping by in_spec value
    #this results in two groups for the points, one group for in spec,
    #one group for out of spec.
    scale_color_manual(values = c('red', 'grey'), guide = FALSE)
    #set the manual color scale so out of spec readings are red and in spec are grey
p

结果图表:

红色突出显示的情节

于 2020-07-22T18:43:50.093 回答