【发布时间】:2020-02-17 03:56:21
【问题描述】:
我想从函数中绘制数据。例如:
制作数据并加载库:
# Load ggplot2
library(ggplot2)
# Create Data
data <- data.frame(
group=LETTERS[1:5],
value=c(13,7,9,21,2)
)
此绘图按预期工作:
# Basic piechart
ggplot(data, aes(x="", y=value, fill=group)) +
geom_bar(stat="identity", width=1, color="white") +
coord_polar("y", start=0) +
theme_void() # remove background, grid, numeric labels
但如果我尝试从函数内部进行绘图:
a <- function(data)
{
# Basic piechart
ggplot(data, aes(x="", y=value, fill=group)) +
geom_bar(stat="identity", width=1, color="white") +
coord_polar("y", start=0) +
theme_void() # remove background, grid, numeric labels
return()
}
a(data)
它只是给我输出:
NULL
不画任何情节。
问题:在我的示例中如何从函数中绘制绘图?
【问题讨论】:
标签: r function ggplot2 functional-programming