我从a中提取路径时遇到了麻烦,并且遇到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))
Run Code Online (Sandbox Code Playgroud)
当我na.approx用于插值它给我错误?否则不是.
约(x [!na],y [!na],xout,...)中的错误:需要至少两个非NA值进行插值此外:警告消息:在xy.coords(x,y)中:NAs强制引入
与这两个data.frame结合起来.每个FF部分都绘制在一个单独的图中,并且只显示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)
Run Code Online (Sandbox Code Playgroud)
错误
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...
Run Code Online (Sandbox Code Playgroud)
出现错误(在帖子的标题中),因为你有太多的参数ggplot.作为问题注释的注释,管道%>%隐含地将管道左侧的输出作为右侧函数的第一个参数.
# these have the same meaning
f(x, y)
x %>% f(y)
Run Code Online (Sandbox Code Playgroud)
此代码复制相同类型的错误.(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
Run Code Online (Sandbox Code Playgroud)
从概念上讲 - 如果你"删掉"东西 - 正在执行的是以下内容:
ggplot(filter(mtcars, am == 1), mtcars)
Run Code Online (Sandbox Code Playgroud)
该ggplot函数假定第一个参数是data参数,第二个参数是aes美学映射.但是在你的管道中,前两个参数是数据帧.这是错误的来源.
解决方案是删除冗余数据参数.更一般地说,我将我的数据转换管道(%>%链)与我的ggplot绘图构建(+链)分开.
| 归档时间: |
|
| 查看次数: |
37113 次 |
| 最近记录: |