R - 在 r 的数据框中可用的连续行对中绘制数据对
R - Plot pairs of data from pairs of sequential rows where available in a data frame in r
我有一个包含多个人的时间序列数据的数据框。数据包括每个个体随时间推移的水面间隔和潜水间隔。对于每个水面间隔,我想使用 ggplot 绘制水面间隔的持续时间与上一次潜水的持续时间(如果可用)。如果连续有两个水面间隔,我想忽略它们,只绘制在它们前面有潜水的表面。我想按个人 ID 执行此操作。
我在下面提供了一些示例数据:
我更愿意为个人使用 dplyr 包 group_by() 函数,但不确定如何 select 每次潜水并将其与以下(后续)浮出水面配对。
df <- data.frame(ID=c("A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B"),
What=c("Dive", "Surface", "Dive","Surface","Dive", "Surface", "Surface", "Dive", "Surface", "Dive", "Surface", "Dive", "Dive", "Surface", "Dive", "Surface", "Dive", "Surface"),
Start=c("2010-05-09 17:29:45", "2010-05-09 17:56:24", "2010-05-09 18:22:15", "2010-05-09 18:52:38", "2010-05-09 18:59:02", "2010-05-09 19:24:37","2010-05-09 19:30:00", "2010-05-09 19:30:57", "2010-05-09 19:48:00","2010-05-03 18:49:35", "2010-05-03 18:58:00", "2010-05-03 19:27:51","2010-05-03 19:35:42", "2010-05-03 20:15:41", "2010-05-03 20:24:13","2010-05-03 20:53:32", "2010-05-03 21:01:31", "2010-05-03 21:40:26"),
End=c("2010-05-09 17:56:24", "2010-05-09 18:22:15", "2010-05-09 18:52:38","2010-05-09 18:59:02", "2010-05-09 19:24:37", "2010-05-09 19:29:28","2010-05-09 19:30:57", "2010-05-09 19:48:00", "2010-05-09 19:49:02", "2010-05-03 18:58:06", "2010-05-03 19:27:51", "2010-05-03 19:35:42", "2010-05-03 20:15:41", "2010-05-03 20:24:13", "2010-05-03 20:53:32", "2010-05-03 21:01:31", "2010-05-03 21:40:26", "2010-05-03 21:48:44"),
Duration = c(26.65, 25.85, 30.38, 6.40, 25.58, 4.85, 0.95, 17.05, 1.03, 8.52, 29.85, 7.85, 39.98, 8.53, 29.32, 7.98, 38.92, 8.30))
df$Start<-as.POSIXct(df$Start, format = "%Y-%m-%d %H:%M:%S")
df$End<-as.POSIXct(df$End, format = "%Y-%m-%d %H:%M:%S")
我想制作一个 ggplot,x 轴为水面持续时间,y 轴为上一次潜水持续时间。如果连续进行两次潜水,忽略第一次潜水并将第二次潜水与下一次浮出水面进行对比;多个表面也是如此;我只想选择在他们面前有潜水的表面。
如有任何帮助,我们将不胜感激!
我不是 100% 确定你要做什么,但如果我理解正确...我们可以做一些操作来获得一个八行数据框,其中有四个潜水表面对两个人中的每一个:
df2 <-
df %>%
group_by(ID) %>%
filter(What != lead(What) | is.na(lead(What))) %>%
select(ID, What, Duration) %>%
mutate(dive_number = ceiling(row_number() / 2)) %>%
ungroup() %>%
spread(What, Duration)
# A tibble: 8 x 4
ID dive_number Dive Surface
<fct> <dbl> <dbl> <dbl>
1 A 1 26.6 25.8
2 A 2 30.4 6.4
3 A 3 25.6 0.95
4 A 4 17.0 1.03
5 B 1 8.52 29.8
6 B 2 40.0 8.53
7 B 3 29.3 7.98
8 B 4 38.9 8.3
然后你可以绘制结果:
df2 %>%
ggplot(aes(x = Surface, y = Dive, color = ID)) +
geom_point()
我有一个包含多个人的时间序列数据的数据框。数据包括每个个体随时间推移的水面间隔和潜水间隔。对于每个水面间隔,我想使用 ggplot 绘制水面间隔的持续时间与上一次潜水的持续时间(如果可用)。如果连续有两个水面间隔,我想忽略它们,只绘制在它们前面有潜水的表面。我想按个人 ID 执行此操作。 我在下面提供了一些示例数据:
我更愿意为个人使用 dplyr 包 group_by() 函数,但不确定如何 select 每次潜水并将其与以下(后续)浮出水面配对。
df <- data.frame(ID=c("A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B"),
What=c("Dive", "Surface", "Dive","Surface","Dive", "Surface", "Surface", "Dive", "Surface", "Dive", "Surface", "Dive", "Dive", "Surface", "Dive", "Surface", "Dive", "Surface"),
Start=c("2010-05-09 17:29:45", "2010-05-09 17:56:24", "2010-05-09 18:22:15", "2010-05-09 18:52:38", "2010-05-09 18:59:02", "2010-05-09 19:24:37","2010-05-09 19:30:00", "2010-05-09 19:30:57", "2010-05-09 19:48:00","2010-05-03 18:49:35", "2010-05-03 18:58:00", "2010-05-03 19:27:51","2010-05-03 19:35:42", "2010-05-03 20:15:41", "2010-05-03 20:24:13","2010-05-03 20:53:32", "2010-05-03 21:01:31", "2010-05-03 21:40:26"),
End=c("2010-05-09 17:56:24", "2010-05-09 18:22:15", "2010-05-09 18:52:38","2010-05-09 18:59:02", "2010-05-09 19:24:37", "2010-05-09 19:29:28","2010-05-09 19:30:57", "2010-05-09 19:48:00", "2010-05-09 19:49:02", "2010-05-03 18:58:06", "2010-05-03 19:27:51", "2010-05-03 19:35:42", "2010-05-03 20:15:41", "2010-05-03 20:24:13", "2010-05-03 20:53:32", "2010-05-03 21:01:31", "2010-05-03 21:40:26", "2010-05-03 21:48:44"),
Duration = c(26.65, 25.85, 30.38, 6.40, 25.58, 4.85, 0.95, 17.05, 1.03, 8.52, 29.85, 7.85, 39.98, 8.53, 29.32, 7.98, 38.92, 8.30))
df$Start<-as.POSIXct(df$Start, format = "%Y-%m-%d %H:%M:%S")
df$End<-as.POSIXct(df$End, format = "%Y-%m-%d %H:%M:%S")
我想制作一个 ggplot,x 轴为水面持续时间,y 轴为上一次潜水持续时间。如果连续进行两次潜水,忽略第一次潜水并将第二次潜水与下一次浮出水面进行对比;多个表面也是如此;我只想选择在他们面前有潜水的表面。
如有任何帮助,我们将不胜感激!
我不是 100% 确定你要做什么,但如果我理解正确...我们可以做一些操作来获得一个八行数据框,其中有四个潜水表面对两个人中的每一个:
df2 <-
df %>%
group_by(ID) %>%
filter(What != lead(What) | is.na(lead(What))) %>%
select(ID, What, Duration) %>%
mutate(dive_number = ceiling(row_number() / 2)) %>%
ungroup() %>%
spread(What, Duration)
# A tibble: 8 x 4
ID dive_number Dive Surface
<fct> <dbl> <dbl> <dbl>
1 A 1 26.6 25.8
2 A 2 30.4 6.4
3 A 3 25.6 0.95
4 A 4 17.0 1.03
5 B 1 8.52 29.8
6 B 2 40.0 8.53
7 B 3 29.3 7.98
8 B 4 38.9 8.3
然后你可以绘制结果:
df2 %>%
ggplot(aes(x = Surface, y = Dive, color = ID)) +
geom_point()