У меня есть «данные» фрейма данных, и я пытаюсь дать каждой группе переменных свой уникальный цвет. Результат должен выглядеть как «данные2». Я смог использовать базовый R, используя
data$color <- c25[1:length(unique(data$variable))][match(data$variable,
unique(data$variable))]
, но я пытаюсь более комфортно использовать dplyr для выполнения той же задачи. Как я мог манипулировать "data1" для создания "data2" с помощью dplyr?
data <- structure(list(Sample.Number = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20), variable = structure(c(1L, 1L, 1L, 1L, 1L, 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, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("Static", "D10 FB", "D12 FB",
"D14 FB"), class = "factor"), value = c(9.61, 7, 6.59, 6.58,
6, 5.93, 57.5, 45.5, 39.5, 39, 22.5, 21.5, 128.5, 78.5, 71.5,
49, 40.5, 40, 36, 35, 390, 478, 298, 524, 474, 406, 478, 1043,
448, 454, 519, 710, 838, 1481, 737, 305, 668, 1096, 340, 152,
735, 760, 439, 882, 742, 730, 5923, 3697, 806, 927, 1726, 1436,
593, 3545, 1669, 814, 1733, 2333, 819, 260, 586, 854, 506, 1067,
747, 781, 1557, 3807, 1063, 1795, 1534, 2761, 666, 2887, 1737,
1044, 2211, 2544, 1181, 322), color = c("black", "black", "black",
"black", "black", "black", "black", "black", "black", "black",
"black", "black", "black", "black", "black", "black", "black",
"black", "black", "black", "black", "black", "black", "black",
"black", "black", "black", "black", "black", "black", "black",
"black", "black", "black", "black", "black", "black", "black",
"black", "black", "black", "black", "black", "black", "black",
"black", "black", "black", "black", "black", "black", "black",
"black", "black", "black", "black", "black", "black", "black",
"black", "black", "black", "black", "black", "black", "black",
"black", "black", "black", "black", "black", "black", "black",
"black", "black", "black", "black", "black", "black", "black"
)), row.names = c(NA, -80L), class = "data.frame")
c25 <- c("dodgerblue2", "#E31A1C", "green4", "#6A3D9A", "#FF7F00", "black", "gold1",
"skyblue2", "#FB9A99", "palegreen2", "#CAB2D6", "#FDBF6F", "gray70",
"khaki2", "maroon", "orchid1", "deeppink1", "blue1", "steelblue4",
"darkturquoise", "green1", "yellow4", "yellow3", "darkorange4", "brown")
data2 <- structure(list(Sample.Number = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20), variable = structure(c(1L, 1L, 1L, 1L, 1L, 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, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("Static", "D10 FB", "D12 FB",
"D14 FB"), class = "factor"), value = c(9.61, 7, 6.59, 6.58,
6, 5.93, 57.5, 45.5, 39.5, 39, 22.5, 21.5, 128.5, 78.5, 71.5,
49, 40.5, 40, 36, 35, 390, 478, 298, 524, 474, 406, 478, 1043,
448, 454, 519, 710, 838, 1481, 737, 305, 668, 1096, 340, 152,
735, 760, 439, 882, 742, 730, 5923, 3697, 806, 927, 1726, 1436,
593, 3545, 1669, 814, 1733, 2333, 819, 260, 586, 854, 506, 1067,
747, 781, 1557, 3807, 1063, 1795, 1534, 2761, 666, 2887, 1737,
1044, 2211, 2544, 1181, 322), color = c("dodgerblue2", "dodgerblue2",
"dodgerblue2", "dodgerblue2", "dodgerblue2", "dodgerblue2", "dodgerblue2",
"dodgerblue2", "dodgerblue2", "dodgerblue2", "dodgerblue2", "dodgerblue2",
"dodgerblue2", "dodgerblue2", "dodgerblue2", "dodgerblue2", "dodgerblue2",
"dodgerblue2", "dodgerblue2", "dodgerblue2", "#E31A1C", "#E31A1C",
"#E31A1C", "#E31A1C", "#E31A1C", "#E31A1C", "#E31A1C", "#E31A1C",
"#E31A1C", "#E31A1C", "#E31A1C", "#E31A1C", "#E31A1C", "#E31A1C",
"#E31A1C", "#E31A1C", "#E31A1C", "#E31A1C", "#E31A1C", "#E31A1C",
"green4", "green4", "green4", "green4", "green4", "green4", "green4",
"green4", "green4", "green4", "green4", "green4", "green4", "green4",
"green4", "green4", "green4", "green4", "green4", "green4", "#6A3D9A",
"#6A3D9A", "#6A3D9A", "#6A3D9A", "#6A3D9A", "#6A3D9A", "#6A3D9A",
"#6A3D9A", "#6A3D9A", "#6A3D9A", "#6A3D9A", "#6A3D9A", "#6A3D9A",
"#6A3D9A", "#6A3D9A", "#6A3D9A", "#6A3D9A", "#6A3D9A", "#6A3D9A",
"#6A3D9A")), row.names = c(NA, -80L), class = "data.frame")