我在从 a 中提取路径时遇到问题ggplot
并且遇到错误。
下面给出的图像解释了我正在寻找的结果:(在图像编辑器中完成以解释目的)
假设图 1 是我的原始图。我正在寻找的是将第一个点作为“F”点,并从该点出发 24 小时行驶。
Des %>%
mutate(nf = cumsum(ACT=="F")) %>% # build F-to-F groups
group_by(nf) %>%
mutate(first24h = as.numeric((DateTime-min(DateTime)) < (24*3600))) %>% # find the first 24h of each F-group
ggplot(aes(x=Loq, y=Las)) +
geom_path(aes(colour=first24h)) + scale_size(range = c(1, 2))+ geom_point()
Library(zoo)
full.time = seq(Des$DateTime[1], tail(Des$DateTime, 1), by=600) # new timeline with point at every 10 min
d.zoo = zoo(Des[,2:3], Des$DateTime) # convert to zoo object
d.full = as.data.frame(na.approx(d.zoo, xout=full.time)) # interpolate; result is also a zoo object
d.full$DateTime = as.POSIXct(rownames(d.full))
当我使用时na.approx
对于插值它给我错误?否则不会。
approx(x[!na], y[!na], xout, ...) 中的错误:
需要至少两个非 NA 值进行插值
另外:警告消息:
在 xy.coords(x, y) 中:通过强制引入的 NA
有了这两个data.frame
结合起来。每个 F-F 部分都绘制在单独的图中,并且仅显示 F 点之后不超过 24 小时的点
library(dplyr)
library(ggplot)
Des %>%
select(ACT, DateTime) %>%
right_join(d.full, by="DateTime") %>%
mutate(ACT = ifelse(is.na(ACT),"",ACT)) %>%
mutate(nf = cumsum(ACT=="F")) %>%
group_by(nf) %>%
mutate(first24h = (DateTime-min(DateTime)) < (24*3600)) %>%
filter(first24h == TRUE) %>%
filter(first24h == 1) %>%
ggplot(Des, aes(x=Loq, y=Las,colour=ACT)) +
geom_path() + facet_wrap(~ nf)
Error
ggplot.data.frame(., Des, aes(x = Loq, y = Las, color = ACT)) 中的错误:
应使用 aes 或 aes_string 创建映射
这是我的Des
format:
ID Las Loq ACT Time Date
1 12 13 R 23:20 1-1-01
1 13 12 F 23:40 1-1-01
1 13 11 F 00:00 2-1-01
1 15 10 R 00:20 2-1-01
1 12 06 W 00:40 2-1-01
1 11 09 F 01:00 2-1-01
1 12 10 R 01:20 2-1-01
so on...