Question

This is my first question about R in a forum, so sorry in advance if I made any mistake formulating the question or specifying the title.

The point is that for a particular task with ggplot I define the aesthetics outside the ggplot function and then provide it as an argument.

>mytmpaes<-aes(x=Sample,y=ddCt.lin,ymax=ddCt.lin+ddCt.lin.sd,ymin=ddCt.linddCt.lin.sd,fill=factor(endog))
>my.ggplot(x,mytmpaes)

But sometimes I just want to modify some objects of the mytmpaes list without defining all of them againg using aes(). However, I don't really know how to deal with this special list. The aes list looks like this:

>mytmpaes
List of 5
$ x    : symbol Sample
$ y    : symbol ddCt.lin
$ ymax : language ddCt.lin + ddCt.lin.sd
$ ymin : language ddCt.lin - ddCt.lin.sd
$ fill : language factor(Rep)

I figured out how to modify some of them like this:

 > mytmpaes$x<-as.symbol('Names')
 > mytmpaes$fill<-call('factor',quote(target))
 > mytmpaes
 List of 5
  $ x   : symbol Names
  $ y   : symbol ddCt.lin
  $ ymax: language ddCt.lin + ddCt.lin.sd
  $ ymin: language ddCt.lin - ddCt.lin.sd
  $ fill: language factor(endog)

However, I couldn't find the way to modify the ymax or ymin with a similar expression. For example, I would like to change ymax to 'ddCt.log2 - ddCt.log2.sd'.

Can someone give me some advise for it? Also, is there a more correct way to modify the aes list?

Thanks,

Alejandro

Was it helpful?

Solution 2

I think you are looking for substitute:

returns the parse tree for the (unevaluated) expression expr, substituting any variables bound in env

And by way of an example:

df <- data.frame(gp = factor(rep(letters[1:3], each = 10)),
                 y = rnorm(30))
##  Make an aes
tmpaes <- aes(x = gp, y = y , ymin = -2 , ymax = 2 )

##  Plot with it
ggplot(df) +
  geom_point( tmpaes )

##  Modify aes with a new variable
new <- 10
tmpaes$ymax <- substitute( new )   

##  replot
ggplot(df) +
  geom_point( tmpaes )

enter image description here

OTHER TIPS

If you do this a lot, I suggest you use a function similar to aes:

aes.update <- function (aes, ...) 
{
  aes_new <- structure(as.list(match.call()[-c(1,2)]), class="uneval")
  aes_new <- ggplot2:::rename_aes(aes_new)
  aes[names(aes_new)] <- aes_new
}

Then you can just update it all at once

mytmpaes_new <- aes.update(mytmpaes, x=Names, ymax=ddCt.log2 - ddCt.log2.sd)

Here is the easiest way:

> a <- aes(x=Sample,y=ddCt.lin,ymax=ddCt.lin+ddCt.lin.sd,ymin=ddCt.linddCt.lin.sd,fill=factor(endog))
> a
List of 5
 $ x   : symbol Sample
 $ y   : symbol ddCt.lin
 $ ymax: language ddCt.lin + ddCt.lin.sd
 $ ymin: symbol ddCt.linddCt.lin.sd
 $ fill: language factor(endog)
> a$ymax <- aes(ddCt.log2 - ddCt.log2.sd)[[1]]
> a
List of 5
 $ x   : symbol Sample
 $ y   : symbol ddCt.lin
 $ ymax: language ddCt.log2 - ddCt.log2.sd
 $ ymin: symbol ddCt.linddCt.lin.sd
 $ fill: language factor(endog)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top