r - Evaluating ddply within a function when passed ggplot2 aesthetic mappings as arguments -


i'm working on function create bean plots in ggplot2 , stuck on step calculating medians each group. i've tried couple of solutions @ object not found error ddply inside function , object not found error ddply inside function still unable work. error message returned "error in as.quoted(.variables) : object 'iv' not found" indicates it's not evaluting symbol iv in correct environment. if possible, keep method of passing variables function ggplot2 aesthetic mapping since using same aesthetic mappings violin , rug plots later on in function.

the code function:

 ggbean <- function(data = null, mapping = null, ...){   require(plyr)    x <- mapping[['x']]   y <- mapping[['y']]  # calculate medians each group  medfn <- function(mydat, x1, y1){     z <- do.call("ddply",list(mydat, x1, summarize, med = call("median", y1, na.rm=true)))     return(z)  }  res <- medfn(data, x, y)  } 

sample data

set.seed(1234) single_df <- data.frame(dv = c(rnorm(100), rnorm(100,12,3)),  iv = as.factor(sample(letters[1:4], size = 200, replace = true))) 

call function ggplot2 aesthetics

res3 <- ggbean(data = single_df, aes(x = iv, y = dv)) 

should provide like

  iv      med   1  7.254916   2  b 1.367827   3  c 1.467737   4  d 8.670698 

you'll find life easier if "render" aesthetics on. don't need special ddply calls:

library(ggplot2) library(plyr)  ggbean <- function(data, mapping, ...) {   df <- quickdf(eval.quoted(mapping, data))   ddply(df, "x", summarise, med = median(y, na.rm = true)) }  single_df <- data.frame(   dv = c(rnorm(100), rnorm(100, 12, 3)),    iv = sample(letters[1:4], size = 200, replace = true) )  res3 <- ggbean(data = single_df, aes(x = iv, y = dv)) 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -