【问题标题】:How can I pass a variable argument to the color option in ggplot如何将变量参数传递给 ggplot 中的颜色选项
【发布时间】:2014-05-12 19:39:24
【问题描述】:

在函数底部第三行的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")

这是数据框的一个小样本,我希望它可以用于“可重现的示例”。

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")

【问题讨论】:

  • 对不起,如果我没有很好地说明问题。这是我第一次使用堆栈溢出。我很快就收到了 2 张反对票,所以我一定是把这个问题说得很糟糕。有什么建议么?我已经使用 R 很多年了,并且已经“研究”了这个问题一个多小时。我可能犯了一个愚蠢的错误......
  • reproducible example 会有很大帮助。
  • ?aes_string 也可能有用。
  • 本- 非常感谢。 aes_string 工作,使用代码: x
  • (1) 这个问题现在有了很大的改善,下次请注意格式化技术,(2) aes_string 之所以存在,是因为eval(parse(...)) 几乎总是一个坏主意。见library(fortunes); fortune(106)。 (3) 请随时在下面的答案中写下对您有用的内容,然后(在短暂的等待期后)接受它。

标签: r function ggplot2


【解决方案1】:

我会这样做,使用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")

我收到警告,但我认为这是由于使用了一小部分数据...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多