ggplot.data.frame中的错误:应该使用aes或aes_string创建映射 [英] Error in ggplot.data.frame : Mapping should be created with aes or aes_string
问题描述
从 ggplot
提取路径时遇到麻烦,并陷入了错误。
I am having a trouble while extracting the path from a ggplot
and am stuck with an error.
下面给出的图像解释了我正在寻找的结果:(在图像编辑器中完成以解释目的)
The image given below explains the result I am looking for: (Done in image editor for explaining purpose)
让我们假设图1是我的原始图。我要寻找的是将第一点称为 F点,并从该点出发行驶24小时。
Let's assume that Plot 1 is my original plot. What I am looking for is taking first point as 'F' point and traveling 24hrs from that point.
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 ?? ,否则不会。
When I am using na.approx
for interpolation it is giving me Error?? Otherwise not.
大约错误(x [!na],y [!na],xout,...):
需要至少两个非用于插入
的NA值另外:警告消息:
在xy.coords(x,y)中:强制引入的NAs
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
组合在一起。每个FF部分均在单独的绘图中绘制,并且仅显示不超过F点后24小时的点
With these two data.frame
s combined. Every F-F section is drawn in a separate plot and only the points not longer than 24h after the F-point is shown
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
Error in ggplot.data.frame(., Des, aes(x = Loq, y = Las, colour = ACT)) : Mapping should be created with aes or 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
的参数过多,导致出现错误(在帖子的标题中)。作为对问题注释的注释,管道%>%
隐式包含管道左侧的输出,作为右侧函数的第一个参数
The error (in the title of the post) arises because you have too many arguments to ggplot
. As the comments to the question note, the pipeline %>%
implicitly includes the output from the left-hand side of the pipe as the first argument to the function on the righthand side.
# these have the same meaning
f(x, y)
x %>% f(y)
此代码复制了相同类型的错误。 (为清楚起见,我已经将 aes
映射到其自己的步骤。)
This code replicates the same kind of error. (I've separated out the aes
mapping to its own step for clarity.)
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
从概念上讲-如果您解开东西-到底是什么执行以下命令:
Conceptually--if you "unpipe" things--what's being executed is the something like following:
ggplot(filter(mtcars, am == 1), mtcars)
ggplot
函数假定第一个参数为 data
参数,第二个是 aes
美学映射。但是在您的管道中,前两个参数是数据帧。这是错误的根源。
The ggplot
function assumes the first argument is the data
parameter and the second is an aes
aesthetic mapping. But in your pipeline, the first two arguments are data frames. This is the source of the error.
解决方案是删除冗余数据参数。更一般而言,我将数据转换管道(%>%
链)与我的 ggplot
绘图构建区( +
链)。
The solution is to remove the redundant data argument. More generally, I separate my data transformation pipeline (%>%
chains) from my ggplot
plot building (+
chains).
这篇关于ggplot.data.frame中的错误:应该使用aes或aes_string创建映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!