ggplot.data.frame 中的错误:应使用 aes 或 aes_string 创建映射

2024-06-24

我在从 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...

出现错误(在帖子标题中)是因为您有太多参数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)

The ggplot函数假设第一个参数是data参数,第二个是aes美学映射。但在您的管道中,前两个参数是数据帧。这就是错误的根源。

解决方案是删除冗余数据参数。更一般地说,我将数据转换管道分开(%>%链)从我的ggplot情节建设(+链)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ggplot.data.frame 中的错误:应使用 aes 或 aes_string 创建映射 的相关文章

随机推荐