自定义功能,ggplot和返回值(Custom Function, ggplot and retur

2019-06-26 18:26发布

今天,我发现了一些奇怪的。 我写这应该返回一个数据帧和情节,与GGPLOT2产生了情节的功能。

但是,如果我运行的功能,无论是情节将不会出现或数据帧。

你知道这个问题,可以给我一个解决方案呢?

非常感谢你!

莱纳

这是一个虚拟函数,使自己清楚:

dummyfunct<-function(){
df <- data.frame(time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
               total_bill = c(14.89, 17.23))

ggplot(data=df, aes(x=time, y=total_bill)) + geom_bar(aes(fill=time))
return(df)
} 

要么

dummyfunct<-function(){
df <<- data.frame(time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
               total_bill = c(14.89, 17.23))

ggplot(data=df, aes(x=time, y=total_bill)) + geom_bar(aes(fill=time))
}

Answer 1:

我会回答,但我知道这是一个重复的问题,它可能会关闭掉:

有了你需要explicitally使用ggplot print功能在里面:

dummyfunct<-function(){
    df <- data.frame(time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
           total_bill = c(14.89, 17.23))
    x <- ggplot(data=df, aes(x=time, y=total_bill)) + geom_bar(aes(fill=time))
    print(x)
    return(df)
} 

dummyfunct()


文章来源: Custom Function, ggplot and return values
标签: r ggplot2