【问题标题】:wrapping ggplot around a function将 ggplot 包裹在一个函数周围
【发布时间】:2014-05-28 12:33:19
【问题描述】:

我有下面的代码,当我有它时,它似乎运行良好

      p<-ggplot(data=x.all.ER, aes(x=Year, y=value, fill = (factor(x.all.ER$Strategy))))+ 
      geom_bar(stat = 'identity',position = 'dodge', colour = "black") +
      scale_fill_manual(values = mycols) +
      scale_y_continuous(breaks=  seq(-0.3,0.3,by=0.025), labels = percent) +
      ylab("ERET") + theme(axis.title.y = element_text(size=15, face = "bold"))+
      xlab("YEAR") + theme(axis.title.x = element_text(size=15, face = "bold"))
      print(p)

但是当我尝试将它包裹在另一个函数中时,它会给我带来错误。任何帮助是极大的赞赏。谢谢!

      mybar<-function(DS, x, y, fillby, labels, mycols, xlabel, xbreaks, 
                                                            ylabel, title)
          {
     #my.cols =c("#F7FBFF", "#DEEBF7", "#C6DBEF", "#9ECAE1", "#6BAED6", 
             # "#000000","#2171B5")

      p<-ggplot(data=DS, aes(x=x, y=y, fill = fillby)+ 
              geom_bar(stat = 'identity',position = 'dodge', colour = "black") + 
              scale_fill_manual(values = mycols) +
              scale_y_continuous(breaks=  xbreaks, labels = percent) +
              ylab(ylabel) + 
              theme(axis.title.y = element_text(size=15, face = "bold"))+   
              xlab(xlabel) + 
              theme(axis.title.x = element_text(size=15, face = "bold"))
              print(p)
        }

【问题讨论】:

  • 您很可能希望在函数中使用aes_string。也许这个问题可以帮助stackoverflow.com/questions/20494238/…(关于这个问题会有其他/更好的答案)
  • 在你的第一个情节中 - 你在策划什么?它是每个“年”/“战略”组合的最大“价值”吗?
  • 对于初学者,您在调用ggplot(...)时缺少括号

标签: r ggplot2


【解决方案1】:

您可以在 ggplot2 中键入 x=x 而不是 x="x",因为 DS data.frame 本质上是附加在函数中的(请参阅 ?with 以了解正在发生的事情)。您对ggplot 的调用是在DS 数据框中寻找一个名为fillby 的变量,但没有。根据 cmets 的建议,解决此问题的方法是使用 aes_string 函数。

library(ggplot2)
df = data.frame( xx=1:10, yy=1:10+rnorm(10) )

# Regular use of ggplot
ggplot(df,aes(x=xx,y=yy)) + geom_point()

# wrapping ggplot in another function
myggplot = function( DF, x, y) {
  ggplot(df,aes(x=x,y=y)) + geom_point()
}
myggplot(df,x,y)
## Error in eval(expr, envir, enclos) : object 'x' not found

问题在于 ggplot 正在寻找 data.frame 中的 x 列,因为 myggplot 中的 ggplot(df,aes(x=x,y=y))。请注意,这将正常工作。

names(df) = c("x","y")
myggplot(df,x,y)

但是这个也是。

names(df) = c("x","y")
myggplot(df,NULL,NA)

修复:

df = data.frame( xx=1:10, yy=1:10+rnorm(10) )
myggplot2 = function( DF, x, y) {
  ggplot(df,aes_string(x=x,y=y)) + geom_point()
}
myggplot2(df,"xx","yy")

如果你想避免写引号,那么就这样做

myggplot3 = function( DF, x, y) {
  ggplot(df,aes_string(x=deparse(substitute(x)),
                       y=deparse(substitute(y)))) + geom_point()

}
myggplot3(df,xx,yy)

【讨论】:

    【解决方案2】:

    使用aes_string(...) 而不是aes(...),就像在另一个答案中一样,是执行此操作的惯用方式。我实际上更喜欢稍微不同的方法:

    mybar<-function(DS, x, y, fillby, label, mycols, xlabel, xbreaks, 
                    ylabel, title) {
      require(ggplot2)
      gg <- data.frame(x=DS[,x],y=DS[,y],fill=DS[,fillby])
      p<-ggplot(gg, aes(x=x, y=y, fill = fill))+ 
        geom_bar(stat = 'identity',position = 'dodge', colour = "black") + 
        scale_fill_manual(values = mycols) +
        scale_y_continuous(breaks=  xbreaks, labels = percent) +
        ylab(ylabel) + 
        theme(axis.title.y = element_text(size=15, face = "bold"))+   
        xlab(xlabel) + 
        theme(axis.title.x = element_text(size=15, face = "bold"))
      print(p)         
    }
    

    这和你的一样,只是在函数中创建了一个新的 df。这会比较慢(稍微),但优点是您可以将列名或列号作为xyfillby 传递。

    还请注意,您传递了参数 label,但似乎没有对它做任何事情。

    【讨论】:

      猜你喜欢
      • 2013-08-16
      • 1970-01-01
      • 2015-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-09
      • 2021-03-02
      • 2013-01-07
      相关资源
      最近更新 更多