如何编写一个同时返回数据框和 ggplot 的函数?

huangapple go评论101阅读模式
英文:

How do I write a function that returns both a dataframe and a ggplot?

问题

我想编写一个函数,该函数返回一个数据框和一个 ggplot。仅仅在函数中打印 ggplot 是不够的,因为后续需要在 ggarrange() 中使用它。我需要将 ggplot 和数据框都存储在变量中,以便在管道中后续使用。

例如,如果我想返回一个箱线图和最高距离的前 20% 的汽车:

  1. library('ggplot2')
  2. plotCars <- function(d){
  3. dm <- suppressMessages(melt(d))
  4. p <- ggplot(dm, aes(x=variable, y=value, fill=variable)) +
  5. geom_boxplot(width=0.2)
  6. q <- quantile(d$dist, prob=c(0.8))
  7. dq <- d[d$dist > q[1],]
  8. return(list(p, dq))
  9. }
  10. obj <- plotCars(cars)

调用 obj[[1]] 会按预期打印出图形。

但是数据框会存储为输出对象中的单独列。例如,obj[[2]] 给出 $speed 列,obj[[3]] 给出 $dist 列。

这会破坏数据框的结构,并破坏后续使用它的管道的下一步。如何修复这个问题?

英文:

I want to write a function that returns both a dataframe and a ggplot. Simply printing the ggplot in the function is not enough, as it will need to be used later in ggarrange(). I need to store both the ggplot and the dataframe in variables to be used later in a pipeline.

For example, if I want to return a box plot and the top 20% highest-distance cars:

  1. library(&#39;ggplot2&#39;)
  2. plotCars &lt;- function(d){
  3. dm &lt;- suppressMessages(melt(d))
  4. p &lt;- ggplot(dm, aes(x=variable, y=value, fill=variable)) +
  5. geom_boxplot(width=0.2)
  6. q &lt;- quantile(d$dist, prob=c(0.8))
  7. dq &lt;- d[d$dist &gt; q[1],]
  8. return(c(list(p), dq))
  9. }
  10. obj &lt;- plotCars(cars)

Calling obj[[1]] prints the plot as expected.
如何编写一个同时返回数据框和 ggplot 的函数?

But the dataframe gets stored as invidual columns in the output object. E.g. obj[2] gives the $speed column, and obj[3] gives the $dist column.

This ruins the structure of the dataframe and undermines the next steps of the pipeline that use it. How do I fix this?

答案1

得分: 3

这是一个遵循两个评论中所写的函数的示例,Onyambu'sMrFlick's。唯一的区别是它返回一个具有名称的列表,以使事情更加清晰(我希望如此)。
它还在某种程度上简化了函数的代码。

  1. library('ggplot2')
  2. plotCars <- function(d){
  3. dm <- suppressMessages(reshape2::melt(d))
  4. p <- ggplot(dm, aes(x=variable, y=value, fill=variable)) +
  5. geom_boxplot(width=0.2)
  6. i <- d$dist > quantile(d$dist, prob = 0.8)
  7. dq <- d[i, ]
  8. list(plot = p, data = dq)
  9. }
  10. obj <- plotCars(cars)
  11. obj$plot # 作为具有名称的列表成员访问的图表
  12. obj[[1]] # 同一列表成员
  13. obj$data # 作为具有名称的列表成员访问的数据
  14. obj[[2]] # 同一列表成员

注意:此翻译只包括代码和评论的文本部分。

英文:

Here is a function that follows what has been written in two comments, Onyambu's and MrFlick's. The only difference is that it returns a named list, in order to make things more clear (I hope).
It also somewhat simplifies the function's code.

  1. library(&#39;ggplot2&#39;)
  2. plotCars &lt;- function(d){
  3. dm &lt;- suppressMessages(reshape2::melt(d))
  4. p &lt;- ggplot(dm, aes(x=variable, y=value, fill=variable)) +
  5. geom_boxplot(width=0.2)
  6. i &lt;- d$dist &gt; quantile(d$dist, prob = 0.8)
  7. dq &lt;- d[i, ]
  8. list(plot = p, data = dq)
  9. }
  10. obj &lt;- plotCars(cars)
  11. obj$plot # the plot accessed as a named list member
  12. obj[[1]] # same list member
  13. obj$data # the data accessed as a named list member
  14. obj[[2]] # same list member

huangapple
  • 本文由 发表于 2023年6月29日 02:14:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定