如何使代码自动化而不是手动更改值?

时间:2019-02-05 07:59:18

标签: r function ggplot2 automation data-manipulation

我有一个用于冲积ggplot的代码。我想使我的代码成为一个函数,所以它将是自动的,而不是为不同的值编写相同的代码。您唯一需要更改的就是数据,该怎么办?

这是我当前的代码

const clicks = Observable.fromEvent(document, 'click');
clicks.subscribe(x => console.log('Calling my service here'));

我可以将此代码设置为无需手动输入值而仅更改数据即可自动运行绘图的函数吗?

1 个答案:

答案 0 :(得分:1)

大概是这样
(注意:我只是复制了您的代码,尚未测试它是否确实有效)

myfunction <- function( data_ ) {
  ggplot( data_,
     aes(y = data_$Freq, axis1 = data_$a1, axis2 = data_$a2, axis3 = data_$a3,axis4 = data_$a4, axis5 = data_$a5, axis6 = data_$a6, axis7 = data_$a7, axis8 = data_$a8)) +
    geom_alluvium(aes(fill = data_$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")  
}

然后使用

致电
myfunction( data_ = as.data.frame(allu) )