0
我对R相当陌生,希望有人能够在下面的代码中向我解释2件事。R - Mapply功能创建和PDF绘图/ ggplots
- 为什么我需要双括号
{{
围绕剧情让它录制剧情才能重播呢?然后在replayPlot中需要双方括号[[
。 - 为什么我不可以在mapply中使用
$
表示法?它在它之外工作。在适当的R工作中使用$是不是很糟糕?
我的真实代码大得多,所以认为最好能让应用程序工作。
library(ggplot2)
library(gridExtra)
TDSF <- data.frame(Graduation=sample(1950:2010, 30,replace=TRUE),
Donation=sample(10:50000, 30,replace=TRUE),
Start.Year=sample(1950:2010,30,replace=TRUE),
State=sample(state.abb,30,replace=TRUE))
TDSF$Graduation <- as.numeric(as.character(TDSF$Graduation))
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)}
),c("Graduation","Start"),SIMPLIFY = FALSE)
replayPlot(plots2$Graduation[[1]]) #use $ notation
do.call(grid.arrange,plots2$Graduation[2:3])`#use $ notation
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()}
,c("Graduation","Start"))