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

10

我在从 ggplot 中提取路径时遇到了问题,并陷入了错误。

下面给出的图像解释了我正在寻找的结果:(为说明目的而在图像编辑器中完成)

Image

假设图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进行插值时,为什么会出现错误?而在其他情况下则不会。

Error in approx(x[!na], y[!na], xout, ...) : need at least two non-NA values to interpolate In addition: Warning message: In xy.coords(x, y) : NAs introduced by coercion

将这两个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)

错误

在 ggplot.data.frame(., Des, aes(x = Loq, y = Las, colour = 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...

3
尝试在调用 ggplot 函数时去掉 Data 参数。由于使用了管道符号,数据参数会被默认传递,因此你的第一个参数应是 aes。或者你可以在调用 ggplot 之前去掉管道操作符。 - Benjamin
尝试用 . 替换 Data - Jaap
@Jaap inherits(mapping, "uneval") 中出现错误:找不到对象'Des.' - user4993868
你能否在你的问题中粘贴 dput(Des)dput(Des[sample(nrow(Des), 20),]) 的输出结果? - Jaap
你也可以编造一些数据。点击链接查看如何实现的示例。 - Jaap
显示剩余3条评论
2个回答

7

这篇文章标题所提到的错误是由于你在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绘图建设(+链)分开。

0
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))- 使用“。” 来引用数据,因为您不能重复使用。

请将以下与编程相关的内容从英语翻译成中文。仅返回翻译后的文本:尽力使用正确的拼写、标点和语法。 - Armali

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接