我在从ggplot
中提取路径时遇到了麻烦,并且遇到了错误。
下面给出的图像解释了我正在寻找的结果:(在图像编辑器中完成以解释目的)
我们假设Plot 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
进行插值时,它给了我错误?否则不是。
约(x [!na],y [!na],xout,...)中的错误:需要至少两个非NA值进行插值此外:警告消息:在xy.coords(x,y)中:NAs强制引入
将这两个data.frame
s结合起来。每个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)
错误
ggplot.data.frame中出错(。,Des,aes(x = Loq,y = Las,color = ACT)):应使用aes或aes_string创建映射
这是我的Des
格式:
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...
错误(在帖子的标题中)出现是因为你有太多的ggplot
参数。作为对问题说明的评论,管道%>%
隐含地将管道左侧的输出作为右侧函数的第一个参数。
# these have the same meaning
f(x, y)
x %>% f(y)
此代码复制相同类型的错误。 (为了清楚起见,我将aes
映射分离到了自己的步骤。)
mtcars %>%
filter(am == 1) %>%
ggplot(mtcars) +
aes(x = mpg, y = wt) +
geom_point()
#> Error in ggplot.data.frame(., mtcars) :
#> Mapping should be created with aes or aes_string
从概念上讲 - 如果你“删掉”东西 - 正在执行的是以下内容:
ggplot(filter(mtcars, am == 1), mtcars)
ggplot
函数假设第一个参数是data
参数,第二个参数是aes
美学映射。但是在你的管道中,前两个参数是数据帧。这是错误的来源。
解决方案是删除冗余数据参数。更一般地说,我将我的数据转换管道(%>%
链)与我的ggplot
情节建筑(+
链)分开。
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()
在这一点上:ggplot(。,aes(x = Loq,y = Las)) - 使用'。'在你不能加倍时引用数据