0

I asked similar question and was told there's a duplicated question (R ggplot2 aes argument). I changed my code based on that answer but still does not work.

I'm writing a spider plot and can run it (without function). Then I want to write a function named "plottum" which can input some variables and make plots. But now it does not work.

Can anyone help me? Thanks! (I have changed my code a bit)

library(ggplot2)
library(tumgr)
set.seed(1234)
tumorgrowth = sampleData
tumorgrowth = do.call(rbind,
          by(tumorgrowth, tumorgrowth$name,function(subset) within(subset,
             { treatment = ifelse(rbinom(1,1,0.5), "Drug","Control")   
             #random classfied
            o = order(date)
            date = date[o]
            size = size[o]
            baseline = size[1]
            percentChange = 100*(size-baseline)/baseline
            time = ifelse(date > 250, 250, date) ## data censored at 250
            cstatus = factor(ifelse(date > 250, 0, 1))
          })))  

 # Above codes work well, and problem is this plottum function
 plottum = function(data,time,pct,name,censor,treat){

   ggplot(data,aes(x=data[,time],y=data[,pct],group=data[,name]))+
   geom_line(aes(color=data[,treat]))+
   geom_point(aes(shape=data[,censor],color=data[,treat]))
  }
 plottum(tumorgrowth,"time","percentChange","name","cstatus","treatment" )
zx8754
  • 52,746
  • 12
  • 114
  • 209
JohnSun
  • 19
  • 3
  • Thanks for replying! I just tried running the code and it produces the same error. I think the code provided is reproducible. – JohnSun Jul 12 '19 at 09:53
  • 1
    Yes, you are right. I not expected that `sampleData` is part of one of the packages. But your example is not minimal. I suppose the error comes from `quo_name(x=time,y=pct,group=name)` The documentation states: `quo_name(quo)` - there are no other arguments to `quo_name()` (only `quo=`). – jogo Jul 12 '19 at 11:38
  • Yes the problem is quo_name(), but as you can see, i have several arguments including x,y,and group in aes(), so does that mean i cannot use quo_name()? I have changed my code using something like data[,time], but still does not work. – JohnSun Jul 12 '19 at 14:26
  • 1
    First, your code contains unmatched parentheses at the end of `geom_line` and `geom_point`; second, one parentheses too many at the end of `ggplot`. Finally, if you call ggplot with `ggplot(NULL` and not `ggplot(data` it works perfectly fine. With NULL, `data[,pct]` is treated normally and no weird substitution is attempted. – January Jul 12 '19 at 14:33
  • Thanks for your replying and that really help! I'm sorry I have some copy mistakes about the parentheses and I have corrected them. – JohnSun Jul 12 '19 at 14:39

1 Answers1

1

Option 1: use quasiquotation

scatter_by <- function(data, x, y) {
  x <- enquo(x)
  y <- enquo(y)

  ggplot(data, aes(!!x, !!y)) + geom_point()
}

scatter_by(mtcars, disp, drat)

Option 2: aes_string (soft deprecated, meaning time to move start using new methods)

scatter_by <- function(data, x, y) {
  ggplot(data, aes_string(x, y)) + geom_point()
}

scatter_by(mtcars, "disp", "drat")
zx8754
  • 52,746
  • 12
  • 114
  • 209