新ggplot2和自定义boxplot代码

时间:2012-03-06 15:39:21

标签: r ggplot2

我一直在使用来自this post的Kohske代码的修订版本。我为outlier.alpha添加了一个额外的参数。但是,使用新的ggplot2版本,它会给我一个错误。第一个错误:

Error in eval.with.vis(expr, envir, enclos) : 
  could not find function "proto"

由新ggplot的名称间距引起,并由library(proto)修复。然后错误变为:

Error in proto(GeomBoxplot, { : object 'GeomBoxplot' not found

可以使用ggplot:::GeomBoxplot来访问未导出的函数。然而,最后的任务:

geom_boxplot_jitter_outlier <- GeomBoxplotJitterOutlier$build_accessor()

错误:

Error in get(x, envir = this, inherits = inh) : 
  object 'build_accessor' not found

现在,我不知所措!自定义geom代码低于我的sessionInfo()

GeomBoxplotJitterOutlier <- proto(GeomBoxplot, {
   draw <- function (., data, ..., outlier.colour = "black", outlier.shape = 16, 
    outlier.size = 2, outlier.jitter=0,outlier.alpha=1) {
# copy the body of function 'draw' above and paste here.
defaults <- with(data, data.frame(x = x, xmin = xmin, xmax = xmax, 
        colour = colour, size = size, linetype = 1, group = 1, 
        alpha = 1, fill = alpha(fill, alpha), stringsAsFactors = FALSE))
    defaults2 <- defaults[c(1, 1), ]
        if (!is.null(data$outliers) && length(data$outliers[[1]] >= 
        1)) {
            pp<-position_jitter(width=outlier.jitter,height=0)
            p<-pp$adjust(data.frame(x=data$x[rep(1, length(data$outliers[[1]]))], y=data$outliers[[1]]),.scale)
        outliers_grob <- GeomPoint$draw(data.frame(x=p$x, y = p$y, colour = I(outlier.colour), 
            shape = outlier.shape, alpha = outlier.alpha, size = outlier.size, 
            fill = NA), ...)
    }
    else {
        outliers_grob <- NULL
    }
    with(data, ggname(.$my_name(), grobTree(outliers_grob, GeomPath$draw(data.frame(y = c(upper, 
        ymax), defaults2), ...), GeomPath$draw(data.frame(y = c(lower, 
        ymin), defaults2), ...), GeomRect$draw(data.frame(ymax = upper, 
        ymin = lower, defaults), ...), GeomRect$draw(data.frame(ymax = middle, 
        ymin = middle, defaults), ...))))

}

  objname <- "boxplot_jitter_outlier"
  desc <- "Box and whiskers plot with jittered outlier"
  guide_geom <- function(.) "boxplot_jitter_outlier"

})

geom_boxplot_jitter_outlier <- GeomBoxplotJitterOutlier$build_accessor()



> sessionInfo()
R version 2.14.2 (2012-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=C                 LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] graphics  grDevices utils     datasets  stats     grid      methods   base     

other attached packages:
 [1] proto_0.3-9.2     circular_0.4-3    boot_1.3-4        mgcv_1.7-13       stringr_0.6       RPostgreSQL_0.3-2 biglm_0.8        
 [8] DBI_0.2-5         doMC_1.2.3        multicore_0.1-7   foreach_1.3.2     codetools_0.2-8   iterators_1.0.5   splancs_2.01-29  
[15] sp_0.9-91         ellipse_0.3-5     MASS_7.3-16       ggplot2_0.9.0     reshape2_1.2.1    plyr_1.7.1       

loaded via a namespace (and not attached):
 [1] colorspace_1.1-1   compiler_2.14.2    dichromat_1.2-4    digest_0.5.1       lattice_0.20-0     Matrix_1.0-4      
 [7] memoise_0.1        munsell_0.3        nlme_3.1-103       RColorBrewer_1.0-5 scales_0.2.0      

0 个答案:

没有答案