英文:
How do I write a function that returns both a dataframe and a ggplot?
问题
我想编写一个函数,该函数返回一个数据框和一个 ggplot。仅仅在函数中打印 ggplot 是不够的,因为后续需要在 ggarrange()
中使用它。我需要将 ggplot 和数据框都存储在变量中,以便在管道中后续使用。
例如,如果我想返回一个箱线图和最高距离的前 20% 的汽车:
library('ggplot2')
plotCars <- function(d){
dm <- suppressMessages(melt(d))
p <- ggplot(dm, aes(x=variable, y=value, fill=variable)) +
geom_boxplot(width=0.2)
q <- quantile(d$dist, prob=c(0.8))
dq <- d[d$dist > q[1],]
return(list(p, dq))
}
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:
library('ggplot2')
plotCars <- function(d){
dm <- suppressMessages(melt(d))
p <- ggplot(dm, aes(x=variable, y=value, fill=variable)) +
geom_boxplot(width=0.2)
q <- quantile(d$dist, prob=c(0.8))
dq <- d[d$dist > q[1],]
return(c(list(p), dq))
}
obj <- plotCars(cars)
Calling obj[[1]]
prints the plot as expected.
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's 和 MrFlick's。唯一的区别是它返回一个具有名称的列表,以使事情更加清晰(我希望如此)。
它还在某种程度上简化了函数的代码。
library('ggplot2')
plotCars <- function(d){
dm <- suppressMessages(reshape2::melt(d))
p <- ggplot(dm, aes(x=variable, y=value, fill=variable)) +
geom_boxplot(width=0.2)
i <- d$dist > quantile(d$dist, prob = 0.8)
dq <- d[i, ]
list(plot = p, data = dq)
}
obj <- plotCars(cars)
obj$plot # 作为具有名称的列表成员访问的图表
obj[[1]] # 同一列表成员
obj$data # 作为具有名称的列表成员访问的数据
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.
library('ggplot2')
plotCars <- function(d){
dm <- suppressMessages(reshape2::melt(d))
p <- ggplot(dm, aes(x=variable, y=value, fill=variable)) +
geom_boxplot(width=0.2)
i <- d$dist > quantile(d$dist, prob = 0.8)
dq <- d[i, ]
list(plot = p, data = dq)
}
obj <- plotCars(cars)
obj$plot # the plot accessed as a named list member
obj[[1]] # same list member
obj$data # the data accessed as a named list member
obj[[2]] # same list member
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论