我有一个输出像这样的数据帧的函数:
df:
# A tibble: 100 x 7
test_idx train_user_id test_user_id train_idx D isSame D_rank
<int> <int> <int> <int> <dbl> <lgl> <dbl>
1 5 1 1 2 51.3 TRUE 1
2 1 1 1 4 53.0 TRUE 1
3 5 1 1 4 56.9 TRUE 2
4 1 1 1 5 61.6 TRUE 2
5 1 1 1 3 63.1 TRUE 3
6 1 1 1 2 66.7 TRUE 4
7 5 1 1 5 66.8 TRUE 3
8 5 1 1 3 67.7 TRUE 4
9 9 1 2 4 85.3 FALSE 1
10 1 1 1 1 86.1 TRUE 5
# ... with 90 more rows
现在,我想将不同的D_ranks传递给ggplot以便为1-5中的每一个绘制一个合适的图:
for i = 1:5:
ggplot(df %>% filter(D_rank == i), aes(x = D, fill = isSame)) +
geom_histogram(position = "dodge", bins = 50)
在这种情况下,是否有任何类似地图的函数可以执行此操作并生成5个图(通常为N)?
请告知我如何使用功能编程技术来做到这一点。
数据
df <-
structure(list(test_idx = c(5L, 1L, 5L, 1L, 1L, 1L, 5L, 5L, 9L, 1L, 2L, 9L, 2L, 4L, 5L, 2L,
2L, 7L, 2L, 7L, 4L, 9L, 7L, 3L, 7L,
4L, 9L, 4L, 8L, 9L, 3L, 4L, 6L, 3L, 3L, 6L, 8L, 7L, 8L, 3L, 8L,
6L, 6L, 6L, 8L, 10L, 10L, 10L, 10L, 10L, 9L, 7L, 8L, 7L, 7L,
8L, 6L, 8L, 6L, 5L, 9L, 8L, 9L, 6L, 7L, 6L, 3L, 5L, 2L, 3L, 1L,
4L, 2L, 3L, 1L, 9L, 4L, 2L, 8L, 9L, 2L, 3L, 5L, 3L, 5L, 4L, 7L,
2L, 6L, 1L, 1L, 1L, 4L, 5L, 10L, 4L, 10L, 10L, 10L, 10L),
train_user_id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,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8L, 9L, 6L, 6L, 9L, 7L, 7L, 7L, 6L, 7L, 9L, 6L, 7L,
7L, 7L, 8L, 9L, 10L, 6L),
D = c(51.3423229472633, 53.0102437725091,
56.9151660778132, 61.5523467358413, 63.1196128125039, 66.7242855371326,
66.7691111976302, 67.743214491629, 85.2525195097706, 86.0735262618907,
87.369258138824, 88.6330505796213, 92.2078873349342, 93.7741850369463,
95.1397157285217, 96.2026472448337, 96.9228466441253, 97.2558549437615,
97.3898735538098, 97.5469166830202, 97.5751612411753, 97.9406556127815,
98.9451888862382, 98.9681351156223, 99.766578399879, 100.017199435986,
101.975347576293, 103.097872757076, 105.041685508915, 105.057005748779,
105.496841451745, 106.125579523708, 107.9382547829, 108.612088134172,
108.68293031272, 109.695378380029, 111.325786796424, 111.881553810468,
112.348809216118, 112.888668376943, 113.49128839933, 116.77296297582,
118.888803029784, 120.903835929009, 122.27718661991, 156.423902775119,
167.503309969173, 169.030474185564, 175.189202023024, 180.788562965559,
48.5496720637138, 51.6482423994687, 53.4542060530927, 58.0128616909946,
58.7971141941753, 60.2587315942066, 61.8538635626938, 62.222929248795,
62.3865335970707, 63.0977052264393, 64.9664647267018, 65.5753112984812,
67.0733197397097, 68.7553373383392, 70.3274318273383, 70.9561586831357,
71.1969721821622, 71.9521437091054, 72.2811149112368, 72.7021293248461,
73.1399655726917, 73.4746352292488, 75.5818665806328, 75.6687872897965,
77.5832030879899, 77.9322604688026, 79.7123489004008, 80.1066357588347,
80.1507338530155, 80.4540641900277, 80.5021221779844, 82.5711795226778,
83.1590737400302, 83.279098613812, 83.4857610266722, 83.5491091101213,
83.5564732281585, 84.1257094245289, 84.1402634222038, 88.6441050623217,
90.2958220505986, 90.3323019629058, 91.8753888005348, 92.2496048477023,
92.3323541396661, 93.3607071270989, 96.8024552048253, 102.947477697813,
102.959949397485, 116.434702831121),
isSame = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, TRUE,
TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE,
TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE,
TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE,
FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE),
D_rank = c(1, 1, 2, 2, 3, 4, 3, 4, 1, 5, 1, 2, 2, 1, 5, 3, 4, 1, 5, 2, 2, 3,
3, 1, 4, 3, 4, 4, 1, 5, 2, 5, 1, 3, 4, 2, 2, 5, 3, 5, 4, 3, 4,
5, 5, 1, 2, 3, 4, 5, 1, 1, 1, 2, 3, 2, 1, 3, 2, 1, 2, 4, 3, 3,
4, 4, 1, 2, 1, 2, 1, 1, 2, 3, 2, 4, 2, 3, 5, 5, 4, 4, 3, 5, 4,
3, 5, 5, 5, 3, 4, 5, 4, 5, 1, 5, 2, 3, 4, 5)),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, -100L))
答案 0 :(得分:4)
split
,按df$D_rank
来输入数据,然后尝试imap
。 map
或lapply
也可以使用,但是imap
可以轻松地遍历列表和该列表的名称-名称由.y
捕获(请参见下文) )。这似乎很有用,因为例如可以使用.y
作为情节标题的一部分。
library(purrr)
library(ggplot2)
plot_list <- imap(split(df, df$D_rank), ~ {
ggplot(.x, aes(x = D, fill = isSame)) +
geom_histogram(position = "dodge", bins = 50) +
labs(title = paste0("ID ", .y))
})
plot_list$`5`