如何在折线图中表示 r 中另一列的二进制数据?
how to represent in a line chart, binary data from another column in r?
我有这个 df 并且我为每个唯一的 id 创建了一个折线图。对于每个单独的日期,值列中都有不同的值。 Bin 列中有一个 1 或 o。我想知道我是否可以用另一种颜色表示折线图中对应于 1 值的点。提前谢谢你
dput(df)
structure(list(ID = c("F1", "F1", "F1", "F1", "F1", "F1",
"F1", "F2", "F2", "F2", "F2", "F2", "F2", "F2", "F2", "F3", "F3",
"F3", "F3", "F3", "F3", "F3", "F3", "F3", "F4", "F4", "F4", "F4",
"F4", "F4", "F4", "F4"), Date = c("22/6/2021", "23/6/2021", "24/6/2021",
"25/6/2021", "26/6/2021", "27/6/2021", "28/6/2021", "22/6/2021",
"23/6/2021", "24/6/2021", "25/6/2021", "26/6/2021", "27/6/2021",
"28/6/2021", "29/6/2021", "22/6/2021", "23/6/2021", "24/6/2021",
"25/6/2021", "26/6/2021", "27/6/2021", "28/6/2021", "29/6/2021",
"30/6/2021", "22/6/2021", "23/6/2021", "24/6/2021", "25/6/2021",
"26/6/2021", "27/6/2021", "28/6/2021", "29/6/2021"), Values = c(9.6,
9.8, 10.2, 9.8, 9.9, 9.9, 9.9, 1.2, 1.2, 1.8, 1.5, 1.5, 1.6,
1.4, 1.1, 3266, 3256, 7044, 6868, 6556, 3405, 3410, 3980, 5567,
59.4, 56, 52.8, 52.4, 55.5, 54, 61, 53.6), Bin = c(0L, 0L, 1L,
0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L,
1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L)), class = "data.frame", row.names = c(NA,
-32L))
这是折线图
lst1 <- df %>%
group_split(grp =((match(ID, unique(ID)) -1) %/% 1 + 1))
ggplot(aes(x = factor(Date), y = Values)) +
geom_line(aes(group = ID)) + geom_point() +
facet_wrap(~ID, scales = 'free'))
您可以使用 color
参数:
df %>%
ggplot(aes(x = factor(Date), y = Values)) +
geom_line(aes(group = ID)) +
geom_point(aes(color = as.factor(Bin))) +
facet_wrap(~ID, scales = 'free')
returns
我有这个 df 并且我为每个唯一的 id 创建了一个折线图。对于每个单独的日期,值列中都有不同的值。 Bin 列中有一个 1 或 o。我想知道我是否可以用另一种颜色表示折线图中对应于 1 值的点。提前谢谢你
dput(df)
structure(list(ID = c("F1", "F1", "F1", "F1", "F1", "F1",
"F1", "F2", "F2", "F2", "F2", "F2", "F2", "F2", "F2", "F3", "F3",
"F3", "F3", "F3", "F3", "F3", "F3", "F3", "F4", "F4", "F4", "F4",
"F4", "F4", "F4", "F4"), Date = c("22/6/2021", "23/6/2021", "24/6/2021",
"25/6/2021", "26/6/2021", "27/6/2021", "28/6/2021", "22/6/2021",
"23/6/2021", "24/6/2021", "25/6/2021", "26/6/2021", "27/6/2021",
"28/6/2021", "29/6/2021", "22/6/2021", "23/6/2021", "24/6/2021",
"25/6/2021", "26/6/2021", "27/6/2021", "28/6/2021", "29/6/2021",
"30/6/2021", "22/6/2021", "23/6/2021", "24/6/2021", "25/6/2021",
"26/6/2021", "27/6/2021", "28/6/2021", "29/6/2021"), Values = c(9.6,
9.8, 10.2, 9.8, 9.9, 9.9, 9.9, 1.2, 1.2, 1.8, 1.5, 1.5, 1.6,
1.4, 1.1, 3266, 3256, 7044, 6868, 6556, 3405, 3410, 3980, 5567,
59.4, 56, 52.8, 52.4, 55.5, 54, 61, 53.6), Bin = c(0L, 0L, 1L,
0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L,
1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L)), class = "data.frame", row.names = c(NA,
-32L))
这是折线图
lst1 <- df %>%
group_split(grp =((match(ID, unique(ID)) -1) %/% 1 + 1))
ggplot(aes(x = factor(Date), y = Values)) +
geom_line(aes(group = ID)) + geom_point() +
facet_wrap(~ID, scales = 'free'))
您可以使用 color
参数:
df %>%
ggplot(aes(x = factor(Date), y = Values)) +
geom_line(aes(group = ID)) +
geom_point(aes(color = as.factor(Bin))) +
facet_wrap(~ID, scales = 'free')
returns