【发布时间】:2023-03-13 20:05:01
【问题描述】:
我有一个冲积 ggplot 的代码。我想让我的代码变成一个函数,所以它将是自动的,而不是为不同的值编写相同的代码。您唯一需要更改的就是数据,我该怎么做?
这是我当前的代码
ggplot(as.data.frame(allu), aes(y = allu$Freq, axis1 = allu$a1, axis2 = allu$a2, axis3 = allu$a3,axis4 = allu$a4, axis5 = allu$a5, axis6 = allu$a6, axis7 = allu$a7, axis8 = allu$a8)) + geom_alluvium(aes(fill = allu$a1), width = 1/12) + geom_stratum(width = 1/12, fill = "black", color = "grey") + geom_label(stat = "stratum", label.strata = TRUE) + scale_x_discrete(limits = c ("a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8"), expand = c(0.02, 0.02)) + > scale_fill_brewer(type = "qual", palette = "Set1") + ggtitle("CLL")
我可以将此代码制作成一个无需手动输入值而仅更改数据即可自动运行绘图的函数吗?
【问题讨论】:
-
把它变成一个函数,其中
x是数据框。像myFunc <- function(x){ ggplot(as.data.frame(x), aes(y=x$Freq ...这样的东西,只要你的数据框是一致的 -
您想输入相同 shema 的数据帧,还是要为一个数据帧的不同变量运行代码?
-
最后一个!为一个数据帧的不同变量运行代码
-
避免在 aes 中使用“$”。
-
你能让你的例子可重现吗?
allu是什么样的?除了ggplot2,你还导入了哪些包?预期的输出是多少?
标签: r function ggplot2 automation data-manipulation