aes_string geom_text in a function "Object not found"

856 views
Skip to first unread message

Brandon Hurr

unread,
Jan 24, 2011, 8:12:30 AM1/24/11
to ggplot2
I've been building this function which takes a large table of data and plots histograms, scatterplots, a correlation matrix, runs a PCA, plots biplots, score plots and loadings plots, and finally plots the R2VX of the PCA model. It's a bit overgrown and written by a physiologist, but I have this problem which is confusing me. 

The function call looks like this:
Comboplotter(filename="/Users/brandonhurr/Desktop/Laurent Analysis/Practice Tomato Data/tomato.dataw-outNAs.txt", colstart=3, standard.names=c("2421-1-M:99-B-2-B-"), start.nPCs= 15, text.labels=c("Abbrcd"), mol.type.file="/Users/brandonhurr/Desktop/Laurent Analysis/Practice Tomato Data/tomato.mol.typew-outNAs.txt", cor.limit=0.8, pval.limit=0.001)

When I run it, I get this error. 
[1] "YOU HAVE SUPPLIED A TAB DELIMITED TEXT FILE FOR ANALYSIS"
[1] "THE FOLLOWING ROWS HAVE INCOMPLETE DATA THAT WILL BE REMOVED BEFORE ANALYSIS"
[1] "160"
[1] "SUPPLIED VARIABLES ARE NUMERIC, PROCEEDING WITH CALCULATIONS"
[1] "STANDARD 2421-1-M:99-B-2-B- FOUND."
[1] "FACTOR FOR TEXT LABELS Abbrcd FOUND."
[1] "REPLICATION UNDEFINED, NOT COMPUTING AVERAGES"
Error in paste(standard.names) : object 'standard.names' not found

Which I isolated to this bit of code (Full code attached).

#Density/Distribution plot

if (length(standard.names) == 1){

melon.data.standard.avg<- ddply(melon.data.standard, paste(colnames(melon.data)[fac.pos]), colwise(mean))

# droplevels to get rid of excess factor levels for plotting

melon.data.standard.avg<-droplevels(melon.data.standard.avg)

for (i in colstart:colend){

plot<-ggplot()+

geom_histogram(data=melon.data, aes_string(x= paste(colnames(melon.data)[i])))+

geom_vline(data=melon.data.standard.avg, aes_string(xintercept= paste(colnames(melon.data)[i])), colour= "lightblue3")+

theme_bw()+

geom_text(data=melon.data.standard.avg, aes_string(x=paste(colnames(melon.data)[i]), y= "0", label='paste(standard.names)') , angle=90, colour="lightblue3", hjust=0, vjust=0, size=3)

#save plot

ggsave(sprintf("Histogram.%s.%s.%s.png", (i-colstart+1), mol.type$mol.type[(i-colstart+1)], colnames(melon.data)[i]), width=6, height=6)

}

}


standard.names is used to ID standards and mark them on the distribution plot. If I make standard.names longer than 2 it works beautifully, but in a slightly different way. If there are no standards it works great, but I needed to account for only having one standard. When I run this code out of the function, it works. When I run the function with standard.names defined in the global space it works, but when I run it in the function it fails with the above error. 


Does anyone have a clue what is going on because I am truly at a loss? 


Brandon

tomato.dataw-outNAs.txt
ComboPlotter.r
tomato.mol.typew-outNAs.txt

Brandon Hurr

unread,
Jan 24, 2011, 8:15:11 AM1/24/11
to ggplot2
Also, just in case...

> sessionInfo()
R version 2.12.0 (2010-10-15)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_0.8.9     pcaMethods_1.32.0 Rcpp_0.8.9        pls_2.1-0         MASS_7.3-9        Biobase_2.10.0   
[7] reshape_0.8.3     plyr_1.2.1        proto_0.3-8      

Brandon Hurr

unread,
Jan 24, 2011, 9:14:32 AM1/24/11
to ggplot2
Might as well send to everyone. 

On Mon, Jan 24, 2011 at 14:11, Brandon Hurr <brando...@gmail.com> wrote:
Dennis, 

Yeah, I had everything in pieces and then put it together this last weekend. Each piece is fairly modular and I probably should have just cut out everything that wasn't necessary. Sorry about that. 

When I tried this: 
label = bquote(.(standard.names))

I get a different error.
 
Error in parse(text = x) : 
  <text>:2:0: unexpected end of input
1: 2421-1-M:99-B-2-B-
  ^


When I tried this: 

label = "substitute(standard.names)" #quotes because of aes_string()

Error: geom_text requires the following missing aesthetics: label

When I tried this:
label = deparse(substitute(standard.names))
It didn't cause an error, but the label ended up being plotted as "standard.names."

Another possibility is that at the point where standard.names is accessed, the calling environment of the function is not 'visible' to the environment where standard.names is needed.
I thought that too, but it's clearly using standard.names when it's determining if (length(standard.names) == 1){

I sort of fixed it using the dataframe I already made. I'm not sure why I didn't use it before. 

if (length(standard.names) == 1){

melon.data.standard.avg<- ddply(melon.data.standard, paste(colnames(melon.data)[fac.pos]), colwise(mean))

# droplevels to get rid of excess factor levels for plotting

melon.data.standard.avg<-droplevels(melon.data.standard.avg)

melon.data.standard.avg[fac.pos,1]<-standard.names

for (i in colstart:colend){

plot<-ggplot()+

geom_histogram(data=melon.data, aes_string(x= paste(colnames(melon.data)[i])))+

geom_vline(data=melon.data.standard.avg, aes_string(xintercept= paste(colnames(melon.data)[i]), colour= paste(colnames(melon.data)[fac.pos])))+

theme_bw() #+

#geom_text(data=melon.data.standard.avg, aes_string(x=paste(colnames(melon.data)[i]), y= "0", label = "deparse(standard.names)") , angle=90, colour="lightblue3", hjust=0, vjust=0, size=3)

#save plot

ggsave(sprintf("Histogram.%s.%s.%s.png", (i-colstart+1), mol.type$mol.type[(i-colstart+1)], colnames(melon.data)[i]), width=6, height=6)

}

} else {...



Works now, but I still don't understand what I was doing wrong. 


Thanks for your efforts Dennis. 


Brandon


On Mon, Jan 24, 2011 at 13:42, Dennis Murphy <djm...@gmail.com> wrote:
label = bquote(.(standard.names))


Hadley Wickham

unread,
Jan 29, 2011, 1:19:56 PM1/29/11
to Brandon Hurr, ggplot2
This is a bug with the way that aes_string (and aes) work - they
currently don't capture the environment in which they are created, so
they don't have access to variables that aren't in the global
environment. This is something I hope to fix in a future version -
it's something I've only recently figured out how to properly.

Hadley

> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages