我有两个模型,分别是2和3
。我有10个测试数据集
。对于每个模型和每个数据集,我应用了不同的阈值(每个测试数据集有8个阈值)。我还为每个测试数据集计算了真正率、假阳率等
。
我使用的代码如下:
auc_graph <- calculation_information[1:10, ] %>% ggplot(mapping = aes(x = FPR_All, y = TPR_All, fill = SP_length)) + geom_line()
当我尝试绘制AUC
时,我发现必须手动更改单元格值
,例如[1:10, ]或[11:20,]等
。显然,这不是一个好方法,而且也不可行,因为(我有更多的模型)。
现在,有没有办法可以自动更改条件
的单元格值,并且能够自动保存图表一个接一个
?或者有什么想法可以解决这些问题吗?
可复现的数据集
structure(list(SP_length = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), Test_dataset = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), Prediction_Threshold = c(1.01590126290632, 1.11590126290632, 1.21590126290632, 1.31590126290632, 1.41590126290632, 1.51590126290632, 1.61590126290632, 1.71590126290632, 1.73978185992124, 1.83978185992124, 1.93978185992124, 2.03978185992124, 2.13978185992124, 2.23978185992124, 2.33978185992124, 1.01590126290632, 1.11590126290632, 1.21590126290632, 1.31590126290632, 1.41590126290632, 1.51590126290632, 1.61590126290632, 1.71590126290632, 1.81590126290632, 1.80215326487164, 1.90215326487164, 2.00215326487164, 2.10215326487164, 2.20215326487164, 2.30215326487164, 2.40215326487164, 1.01590126290632, 1.11590126290632, 1.21590126290632, 1.31590126290632, 1.41590126290632, 1.51590126290632, 1.61590126290632, 1.71590126290632, 1.81590126290632, 1.91590126290632, 1.73978185992124, 1.83978185992124, 1.93978185992124, 2.03978185992124, 2.13978185992124, 2.23978185992124, 2.33978185992124, 2.43978185992124, 2.53978185992124), TPR_All = c(1, 1, 0.916372202591284, 0.273262661955241, 0.113074204946996, 0.0577149587750294, 0.0188457008244994, 0.00471142520612485, 1, 0.555555555555556, 0.333333333333333, 0.222222222222222, 0.111111111111111, 0.111111111111111, 0, 1, 1, 0.910377358490566, 0.274764150943396, 0.108490566037736, 0.0577830188679245, 0.0188679245283019, 0.00943396226415094, 0.00117924528301887, 1, 0.444444444444444, 0.333333333333333, 0.111111111111111, 0, 0, 0, 1, 1, 0.895610913404508, 0.230130486358244, 0.107947805456702, 0.0557532621589561, 0.0166073546856465, 0.0118623962040332, 0.00474495848161329, 0.00118623962040332, 1, 0.8, 0.5, 0.5, 0.3, 0.2, 0.2, 0.2, 0.1), FPR_All = c(1, 0.999260901699926, 0.920177383592018, 0.212860310421286, 0.0307957625030796, 0.00394185760039419, 0, 0, 1, 0.871914609739827, 0.244162775183456, 0.0907271514342895, 0.0433622414943296, 0.00733822548365577, 0.00333555703802535, 1, 0.999266503667482, 0.896332518337408, 0.211735941320293, 0.0371638141809291, 0.0039119804400978, 0, 0, 0, 1, 0.42235609103079, 0.171352074966533, 0.0796519410977242, 0.0307898259705489, 0.0100401606425703, 0.00267737617135207, 1, 0.99927728258251, 0.90966032281378, 0.215851602023609, 0.0298723199229101, 0.00433630450493857, 0, 0, 0, 0, 1, 0.880108991825613, 0.335149863760218, 0.0831062670299728, 0.0333787465940054, 0.0143051771117166, 0.00136239782016349, 0, 0)), row.names = c(NA, 50L), class = "data.frame")
回答:
看起来使用facet_grid
非常适合这种情况:
ggplot(calculation_information, mapping = aes(x = FPR_All, y = TPR_All, color = SP_length)) + geom_line(show.legend = FALSE) + facet_grid(SP_length ~ Test_dataset, labeller = labeller(Test_dataset = function(x)paste0("Test Dataset ",x), SP_length = function(x)paste0("SP Length ",x)))
你也可以使用facet_wrap
,它可以控制行列的数量,但facet条的定位会更加困难:
ggplot(calculation_information, mapping = aes(x = FPR_All, y = TPR_All, color = SP_length)) + geom_line(show.legend = FALSE) + facet_wrap(SP_length ~ Test_dataset, ncol = 2, labeller = labeller(Test_dataset = function(x)paste0("Test Dataset ",x), SP_length = function(x)paste0("SP Length ",x)))