ggplot2 - geom_ribbon bug?

SFu*_*n28 2 r ggplot2

这段代码抛出一个错误,我无法弄清楚为什么......

library( plyr )
library( ggplot2 )
library( grid )
library( proto )

# the master dataframe
myDF = structure(list(Agg52WkPrceRange = c(2L, 2L, 2L, 2L, 2L, 2L, 3L, 
5L, 3L, 5L, 3L, 5L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 4L, 3L, 4L, 3L, 4L, 4L, 4L, 4L), OfResidualPntReturn52CWk = c(0.201477324, 
0.22350293, 0.248388728, 0.173871456, 0.201090654, 0.170666183, 
0.18681883, 0.178840521, 0.159744891, 0.129811042, 0.13209741, 
0.114989407, 0.128347625, 0.100945992, 0.057017002, 0.081123718, 
0.018900252, 0.021784814, 0.081931816, 0.059067844, 0.095879746, 
0.038977508, 0.078895248, 0.051344317, 0.077515295, 0.011776214, 
0.099216033, 0.054714439, 0.022879951, -0.079558277, -0.050889584, 
-0.006934821, -0.003407085, 0.032545474, -0.003387139, 0.030418511, 
0.053942523, 0.051398537, 0.073482355, 0.087963039, 0.079555591, 
-0.040490418, -0.130754663, -0.125826649, -0.141766316, -0.150708718, 
-0.171906882, -0.174623614, -0.212945405, -0.174480554), IndependentVariableBinned = structure(c(1L, 
1L, 1L, 1L, 1L, 2L, 3L, 10L, 3L, 10L, 4L, 10L, 4L, 2L, 4L, 4L, 
4L, 5L, 2L, 2L, 2L, 3L, 3L, 5L, 5L, 5L, 5L, 6L, 3L, 6L, 6L, 6L, 
6L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 8L, 9L, 9L, 9L, 9L, 
10L, 10L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8", 
"9", "10"), class = "factor")), .Names = c("Agg52WkPrceRange", 
"OfResidualPntReturn52CWk", "IndependentVariableBinned"), row.names = 28653:28702, class = "data.frame")

# secondary data frame
meansByIndependentVariableBin = ddply( myDF , .( IndependentVariableBinned ) , function( df ) mean( df[[ "OfResidualPntReturn52CWk" ]] ) )

# construct the plot
thePlot = ggplot( myDF , aes_string( x = "IndependentVariableBinned" , y = "OfResidualPntReturn52CWk" ) )
thePlot = thePlot + geom_point( data = meansByIndependentVariableBin , aes( x = IndependentVariableBinned , y = V1 ) )
thePlot = thePlot + geom_line( data = meansByIndependentVariableBin , aes( x = IndependentVariableBinned , y = V1 , group = 1 )  )
thePlot = thePlot + geom_ribbon( data = meansByIndependentVariableBin , aes( group = 1 ,  x = IndependentVariableBinned , ymin = V1 - 1 , ymax = V1 + 1 ) )

# print - error!
print( thePlot )
Run Code Online (Sandbox Code Playgroud)

我试过/没有group = 1.错误是:

Error in eval(expr, envir, enclos) : 
  object 'OfRelStrength52CWk' not found
Run Code Online (Sandbox Code Playgroud)

但不确定这是怎么相关的?我一定错过了一些明显的东西.拿掉最后一个geom(功能区),它就可以了!

Ram*_*ath 13

没有错误geom_ribbon.您的错误是因为您正在y = OfResidualPntReturn52CWkggplot调用中定义,因此geom_ribbon正在寻找它.由于您传递的是不同的数据帧geom_ribbon,因此存在混淆,因而存在错误.从您的绘图调用中,虽然您y = OfResidualPntReturn52CWk在ggplot调用中使用,但是没有您调用它的层,因此它对于绘图是无关紧要的.

这是如何正确地做到这一点(如果我理解你打算在这个情节中做什么)

MIVB    = meansByIndependentVariableBin
thePlot = ggplot(myDF , aes(x = IndependentVariableBinned)) +
  geom_point(aes(y = OfResidualPntReturn52CWk)) +
  geom_point(data = MIVB, aes(y = V1), colour = 'red') + 
  geom_line(data = MIVB , aes(y = V1, group = 1), colour = 'red') +
  geom_ribbon(data = MIVB, aes(group = 1, ymin = V1 - 1 , ymax = V1 + 1), 
     alpha = 0.2)
Run Code Online (Sandbox Code Playgroud)

这是它产生的输出 在此输入图像描述

这是另一种方法,无需提前计算方法.另外我在功能区中使用了均值+ - 标准误差,因为我发现选择+ - 1是任意的

myDF$IndependentVariableBinned = as.numeric(myDF$IndependentVariableBinned)
thePlot = ggplot(myDF , aes(x = IndependentVariableBinned, y = 
   OfResidualPntReturn52CWk)) +
   geom_point() +
   geom_point(stat = 'summary', fun.y = 'mean', colour = 'red') + 
   geom_line(stat = 'summary', fun.y = 'mean', colour = 'red') +
   geom_ribbon(stat = 'summary', fun.data = 'mean_se', alpha = 0.2)
Run Code Online (Sandbox Code Playgroud)

这产生了 在此输入图像描述