【问题标题】:Adding to ggplot mean and SD plot添加到 ggplot 均值和 SD 图
【发布时间】: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
  • 不,很遗憾,这不是问题所在。我已经改变了它,但仍然得到同样的错误。

标签: r ggplot2 summary


【解决方案1】:

我想我知道了,我不需要使用摘要,有内置函数。

p2 <- ggplot()+
  geom_point(data = ss, aes(x = GLDAS, y = L5)) +
  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.5) + ylim(0,0.5) +  labs(x="GLDAS [mm/hr]", y="L5 [mm/hr]" ) +
  theme(text = element_text(size=16))

p <- p2 + stat_summary(data = ss, aes(x = pos, y = L5),
                       fun.y = 'mean', fun.ymin = function(x) 0, geom = 'point', 
                       position = 'dodge') +
          stat_summary(data = ss, aes(x = pos, y = L5), 
                       fun.y = mean,
                       fun.ymin = function(y) mean(y) - sd(y), 
                       fun.ymax = function(y) mean(y) + sd(y), 
                       color = "red", 
               geom ="pointrange",show.legend = FALSE)
p 

【讨论】:

    猜你喜欢
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 2018-06-23
    • 2014-06-10
    • 2019-01-18
    • 1970-01-01
    相关资源
    最近更新 更多