Modify aes list outside ggplot function

耗尽温柔 提交于 2019-12-12 15:55:57

问题


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


回答1:


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 )




回答2:


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)



回答3:


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)


来源:https://stackoverflow.com/questions/17699448/modify-aes-list-outside-ggplot-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!