转换数据框,以便 geom_line() 可以使用它
Convert dataframe so that it can be consumed by geom_line()
我需要将数据帧转换为类似 tibble 的结构,以便 geom_line()
(ggplot2) 可以使用它来创建线图。我很确定我的问题的解决方案在于 dplyr and/or tidyverse 但我不知道该怎么做。我想转换以下类型的数据框:
dates = c("2022-02-01", "2022-02-05", "2022-02-10")
test_data = data.frame(matrix(ncol = 5, nrow = 3))
colnames(test_data) = c("a", "b", "c", "d", "e")
rownames(test_data) = dates
test_data[c(1:3),] = sample(100,5,1)
至:
new_test_data = data.frame(matrix(ncol = 3, nrow = nrow(test_data) * ncol(test_data)))
colnames(new_test_data) = c("value", "date", "colname")
new_test_data[,1] = c(test_data[,1], test_data[,2], test_data[,3], test_data[,4], test_data[,5])
new_test_data[,2] = rownames(test_data)
new_test_data[,3] = c(rep(colnames(test_data)[1], 3), rep(colnames(test_data)[2], 3),rep(colnames(test_data)[3], 3), rep(colnames(test_data)[4], 3), rep(colnames(test_data)[5], 3))
你可以这样做:
library(dplyr)
library(tibble)
library(tidyr)
test_data %>%
rownames_to_column() %>%
pivot_longer(-1)
#> # A tibble: 15 x 3
#> rowname name value
#> <chr> <chr> <int>
#> 1 2022-02-01 a 5
#> 2 2022-02-01 b 7
#> 3 2022-02-01 c 21
#> 4 2022-02-01 d 100
#> 5 2022-02-01 e 87
#> 6 2022-02-05 a 21
#> 7 2022-02-05 b 100
#> 8 2022-02-05 c 87
#> 9 2022-02-05 d 5
#> 10 2022-02-05 e 7
#> 11 2022-02-10 a 87
#> 12 2022-02-10 b 5
#> 13 2022-02-10 c 7
#> 14 2022-02-10 d 21
#> 15 2022-02-10 e 100
我需要将数据帧转换为类似 tibble 的结构,以便 geom_line()
(ggplot2) 可以使用它来创建线图。我很确定我的问题的解决方案在于 dplyr and/or tidyverse 但我不知道该怎么做。我想转换以下类型的数据框:
dates = c("2022-02-01", "2022-02-05", "2022-02-10")
test_data = data.frame(matrix(ncol = 5, nrow = 3))
colnames(test_data) = c("a", "b", "c", "d", "e")
rownames(test_data) = dates
test_data[c(1:3),] = sample(100,5,1)
至:
new_test_data = data.frame(matrix(ncol = 3, nrow = nrow(test_data) * ncol(test_data)))
colnames(new_test_data) = c("value", "date", "colname")
new_test_data[,1] = c(test_data[,1], test_data[,2], test_data[,3], test_data[,4], test_data[,5])
new_test_data[,2] = rownames(test_data)
new_test_data[,3] = c(rep(colnames(test_data)[1], 3), rep(colnames(test_data)[2], 3),rep(colnames(test_data)[3], 3), rep(colnames(test_data)[4], 3), rep(colnames(test_data)[5], 3))
你可以这样做:
library(dplyr)
library(tibble)
library(tidyr)
test_data %>%
rownames_to_column() %>%
pivot_longer(-1)
#> # A tibble: 15 x 3
#> rowname name value
#> <chr> <chr> <int>
#> 1 2022-02-01 a 5
#> 2 2022-02-01 b 7
#> 3 2022-02-01 c 21
#> 4 2022-02-01 d 100
#> 5 2022-02-01 e 87
#> 6 2022-02-05 a 21
#> 7 2022-02-05 b 100
#> 8 2022-02-05 c 87
#> 9 2022-02-05 d 5
#> 10 2022-02-05 e 7
#> 11 2022-02-10 a 87
#> 12 2022-02-10 b 5
#> 13 2022-02-10 c 7
#> 14 2022-02-10 d 21
#> 15 2022-02-10 e 100