【发布时间】:2017-08-12 12:21:19
【问题描述】:
我想寻求帮助。 我正在尝试在一张图上绘制数据及其平均值和 SD 值。但是我收到了这个错误
评估错误(替代(列表(...)),
_data,父框架()):
找不到对象“x”
首先我将数据划分为区间,并使用摘要计算区间的平均值和 SD 值。比我尝试绘制数据点(该部分有效)并将平均值和 SD 值图添加到前一个(这里我失败了)。
请帮我解决这个问题。
UPD:好的,我想我应该在 ss 数据集上使用 stat_summary。只是现在不知道该怎么做。任何建议将不胜感激。
这是我的代码:
#Data
s <- data.frame(L5=rnorm(1686, mean=0.3, sd=1.5),
GLDAS=rnorm(1686, mean=0.25, sd=0.8))
#1 )
#Divide data into 0.02 intervals
breaks = seq(from = 0, to = max(s$GLDAS)+0.02, by = 0.02) #intervals
s$group <- cut(s$GLDAS,
breaks = breaks,
labels = seq(from = 1, to = length(breaks)-1, by = 1),
#create label
right = FALSE)
#Assign labels to a value equal to the middle of the interval
pos <- seq(from = breaks[1]+0.02/2, to = max(breaks)-0.02/2, by = 0.02)
group <- seq(from = 1, to = length(breaks)-1, by = 1)
poss <- cbind.data.frame(pos,group)
ss <- merge(s, poss, by = "group")
#Calculate summary
Summary <- ss %>% #
group_by(pos) %>% # the grouping variable
summarise(mean = mean(L5), # calculates the mean of each group
sd = sd(L5), # calculates the standard deviation of each group
n = n(), # calculates the sample size per group
SE = sd(L5)/sqrt(n())) # calculates the standard error of each group
2) #Plot data points
p2 <- ggplot()+
geom_point(data = s, aes(x = GLDAS, y = L5)) +
#geom_smooth(method = "lm", se=FALSE, color="black",
# formula = my.formula) +
stat_poly_eq(formula = my.formula, size = 4,
aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
parse = TRUE) + geom_point()+
geom_abline(intercept=0, slope=1)+
xlim (0,0.6) + ylim(0,0.6) + labs(x="GLDAS [mm/hr]", y="L5 [mm/hr]" ) +
theme(text = element_text(size=16))
3) #plot mean and SD values
p2 + geom_line(data = Summary, aes(x=pos, y=mean), color='blue') +
geom_point(data = Summary, aes(x=pos, y=mean), color='blue')+
geom_errorbar(data = Summary, aes(ymin=mean-sd, ymax=mean+sd), width=.01,
position=position_dodge(0.005), color='blue')
【问题讨论】:
-
在你的
geom_point调用中绘制平均值,你有aes(x=Summary$pos, y = mean)。x=Summary$pos需要更改为x=pos -
不,很遗憾,这不是问题所在。我已经改变了它,但仍然得到同样的错误。