在下面的代码中inA
在函数底部的第三行中无法正确识别/评估。我得到了
Error in parse(text = inA) : object 'inA' not found".
inA
在使用它的上面的其他两行中被认可得很好。我尝试了很多排列。
gcplot2 <- function (inraw,inA,inB){
inwork <- ddply(inraw,
.( eval(parse(text=inA)), eval(parse(text=inB)),BestYr),
summarize,
cases=sum(Cases,na.rm=TRUE),
pop=sum(Pop,na.rm=TRUE),
rate=round(100000*cases/pop,2))
names(inwork)[1] <- inA
names(inwork)[2] <- inB
#problem "inA" is here
x <- ggplot(inwork,aes(BestYr,rate, color=eval(parse(text=inA))))
x <- x + geom_line(size=1.5) + facet_wrap(as.formula(paste0("~ ",inB)))
print(x)
}
gcplot2(inraw=gc.full,"NewRace","Region")
以下是数据框的一小部分示例,我希望可以将其用于&#34;可重复的示例&#34;。
dput(temp2)
structure(list(LHJ = c("SACRAMENTO", "YOLO", "SAN BENITO", "COLUSA",
"STANISLAUS", "SAN DIEGO", "SHASTA", "TULARE", "MONTEREY", "KERN"
), BestYr = c(2010L, 2010L, 2010L, 2012L, 2012L, 2012L, 2011L,
2011L, 2010L, 2010L), Sex = structure(c(2L, 2L, 2L, 2L, 2L, 1L,
2L, 2L, 1L, 1L), .Label = c("F", "M"), class = "factor"), RaceEth = structure(c(3L,
4L, 6L, 2L, 6L, 4L, 4L, 2L, 4L, 4L), .Label = c("A", "B", "H",
"O", "U", "W"), class = "factor"), AgeGrp = structure(c(1L, 4L,
5L, 2L, 7L, 2L, 4L, 2L, 3L, 1L), .Label = c("0-9", "10-14", "15-19",
"20-24", "25-29", "30-34", "35-44", "45+", "Unk"), class = "factor"),
Cases = c(NA, 0, 0, 0, 15.652173913, NA, 0, 0, 0, 0), Pop = c(32752.30608,
538.17138648, 444.83561193, 11.107216039, 14186.950585, 5486.3069863,
338.26814356, 245.3890448, 535.23711331, 2278.6798429), NewRace = c("Hispanic",
"Other", "White", "Black", "White", "Other", "Other", "Black",
"Other", "Other"), Region = structure(c(6L, 6L, 3L, 5L, 3L,
8L, 5L, 3L, 2L, 3L), .Label = c("Bay Area", "Central Coast",
"Central Inland", "Los Angeles", "Northern", "Sacramento Area",
"San Francisco", "Southern"), class = "factor")), .Names = c("LHJ",
"BestYr", "Sex", "RaceEth", "AgeGrp", "Cases", "Pop", "NewRace",
"Region"), row.names = c(41377L, 67523L, 42571L, 7418L, 59857L,
45051L, 54102L, 64260L, 32612L, 17538L), class = "data.frame")
答案 0 :(得分:0)
我会使用aes_string
执行此操作(并通过字符向量而不是ddply
指定.()
变量):
gcplot2 <- function (inraw,inA,inB){
require("plyr")
require("ggplot2")
inwork <- ddply(inraw,
c(inA, inB,"BestYr"),
summarize,
cases=sum(Cases,na.rm=TRUE),
pop=sum(Pop,na.rm=TRUE),
rate=round(100000*cases/pop,2))
x <- ggplot(inwork,aes(BestYr,rate)) +
geom_line( aes_string(color=inA),size=1.5) +
facet_wrap(as.formula(paste0("~ ",inB)))
x
}
gcplot2(inraw=gc.full,"NewRace","Region")
我收到警告,但我认为这是由于使用了一小部分数据......