atclaus - 2 months ago 5x
R Question

# R - Mapply Functionality Creating and PDFing Plots/ggplots

I am fairly new to R and hoping someone can explain 2 things to me in the code below.

1. Why do I need double brackets
`{{`
around the plot to get it to recordPlot so I can replay it? And then I need double square brackets
`[[`
in the replayPlot.

2. Why can I not use the
`\$`
notation inside mapply? It works outside of it. Is it bad to use \$ in "proper" R work?

My real code is much larger so think it best to get mapply to work.

``````library(ggplot2)
library(gridExtra)

Donation=sample(10:50000, 30,replace=TRUE),
Start.Year=sample(1950:2010,30,replace=TRUE),
State=sample(state.abb,30,replace=TRUE))
TDSF\$Start <- as.numeric(as.character(TDSF\$Start))

plots2 <- mapply(function(nm,df.year,df.bracket_5,df.bracket_10) list(
{{plot(-1:1,-1:1,type="n",xaxt="n",yaxt="n",ann=FALSE)+
text(0,0,paste("Analysis by",nm,"Year"),cex=2)
recordPlot()}},
{ggplot(data=TDSF, aes_(x=as.name(nm))) + geom_histogram(color="red",binwidth = 1,boundary=-.01)},
{ggplot(data=TDSF, aes_(x=as.name(nm))) + geom_histogram(color="red",binwidth = 5,boundary=-.01)}

mapply(function(nm)
{pdf(file=paste(nm,"test.pdf"))
replayPlot(plots2[[nm]][[1]]) #use [[]][[]]
do.call(grid.arrange,c(plots2[[nm]][2:3],ncol=1)) #use [[]][[]]
dev.off()}
``````

Let me reformat your code a bit :

``````library(ggplot2)
library(gridExtra)
Donation=sample(10:50000, 30,replace=TRUE),
Start.Year=sample(1950:2010,30,replace=TRUE),
State=sample(state.abb,30,replace=TRUE))
TDSF\$Start <- as.numeric(as.character(TDSF\$Start))

plots2 <- mapply(function(nm,df.year,df.bracket_5,df.bracket_10) list(
{plot(-1:1,-1:1,type="n",xaxt="n",yaxt="n",ann=FALSE)+
text(0,0,paste("Analysis by",nm,"Year"),cex=2)
recordPlot()},
{ggplot(data=TDSF, aes_(x=as.name(nm))) + geom_histogram(color="red",binwidth = 1,boundary=-.01)},
{ggplot(data=TDSF, aes_(x=as.name(nm))) + geom_histogram(color="red",binwidth = 5,boundary=-.01)}

plots2

n <- length(plots2)
nCol <- floor(sqrt(n))

replay <- function(nm)  {
pdf(file = paste(nm,"test.pdf"))
replayPlot(plots2[[nm]][[1]]) #use [[]][[]]
do.call(grid.arrange,c(plots2[[nm]][2:3],ncol = 1)) #use [[]][[]]
dev.off()
}

``````   plots2\$Graduation[[1]] is equivalent to plots2[[1]][[1]] for example