r - Plotting with ggplot through calling another function -
i have function gets matrix 4 columns input. function plots boxplots using 2 columns of input matrix, fills them different color based on third column , plots points on each box based on fourth column show parameters.
p1 <- ggplot(pl1, aes(x=factor(edge_n), y=get(make.names(y_label)), ymax=max(get(make.names(y_label)))*1.05))+ geom_boxplot(aes(fill=method), outlier.shape= na)+ theme(text = element_text(size=20), aspect.ratio=1)+ xlab("number of edges")+ ylab(y_label)+ scale_fill_manual(values=color_box)+ geom_point(aes(x=factor(edge_n), y=get(make.names(true_des)), ymax=max(get(make.names(true_des)))*1.05, color=method), position = position_dodge(width=0.75))+ scale_color_manual(values=color_pnt)
finally @ end of function using print()
function print on opened pdf.
when run function line line works well, when call function function, not work , below error:
error in make.names(true_des) : object 'true_des' not found
actually 5 hours trying solve problem, can't. can help?
your example not clear because give call don't show values of variables it's hard figure out you're trying (for instance, method
name of column in data frame pl1
, or variable (and if it's variable, type? string? name?)).
nonetheless, here's example should set on way doing want:
try this:
pl1 <- data.frame(edge_n = sample(5, 20, true), foo = rnorm(20), bar = rnorm(20)) y_label <- 'foo' ax <- do.call(aes, list( x=quote(factor(edge_n)), y=as.name(y_label), ymax = substitute(max(y)*1.05, list(y=as.name(y_label))))) p1 <- ggplot(pl1) + geom_boxplot(ax) print(p1)
this should started figuring out rest of you're trying do.
alternately (a different interpretation of question) may running problem environment in aes evaluates arguments. see https://github.com/hadley/ggplot2/issues/743 details. if issue, answer might override default value of environment
argument aes
, instance: aes(x=factor(edge_n), y=get(make.names(y_label)), ymax=max(get(make.names(y_label)))*1.05, environment=environment())
Comments
Post a Comment