将 gtExtras::gt_sparkline 应用于多列
Apply gtExtras::gt_sparkline to multiple columns
基于 this link 中 sparkline
的示例代码:
library(gt)
library(gtExtras)
mtcars %>%
dplyr::group_by(cyl) %>%
# must end up with list of data for each row in the input dataframe
dplyr::summarize(mpg_data = list(mpg), .groups = "drop") %>%
gt() %>%
gt_sparkline(mpg_data)
输出:
现在我希望将上面的代码应用到下面的数据 df
中,这意味着将 type
用作 cyl
,其他 year-month 列用作 mpg-data
:
structure(list(type = c("v1", "v2"), `2017-06` = c(244.955, 9e-04
), `2017-07` = c(244.786, -7e-04), `2017-08` = c(245.519, 0.003
), `2017-09` = c(246.819, 0.0053), `2017-10` = c(246.663, -6e-04
)), class = "data.frame", row.names = c(NA, -2L))
我怎样才能做到这一点?谢谢。
编辑:
data <- melt(df, id = 'type')
data %>%
dplyr::group_by(type) %>%
# must end up with list of data for each row in the input dataframe
dplyr::summarize(values = list(value), .groups = "drop") %>%
gt() %>%
gt_sparkline(values)
输出:
通过添加 value
列编辑数据:
structure(list(type = c("v1", "v2"), `2017-06` = c(244.955, 9e-04
), `2017-07` = c(244.786, -7e-04), `2017-08` = c(245.519, 0.003
), `2017-09` = c(246.819, 0.0053), `2017-10` = c(246.663, -6e-04
), value = c(1.2, 1.6)), class = "data.frame", row.names = c(NA, -2L))
您可以使用 rowwise
并折叠列表中行的所有数据。
library(dplyr)
library(gt)
library(gtExtras)
df %>%
rowwise() %>%
mutate(data = list(c_across(-type))) %>%
select(type, data) %>%
gt() %>%
gt_sparkline(data)
我们可以使用 pmap
来自 purrr
library(gt)
library(gtExtras)
library(dplyr)
library(purrr)
df %>%
transmute(type, data = pmap(across(-type), list)) %>%
gt() %>%
gt_sparkline(data)
输出:
基于 this link 中 sparkline
的示例代码:
library(gt)
library(gtExtras)
mtcars %>%
dplyr::group_by(cyl) %>%
# must end up with list of data for each row in the input dataframe
dplyr::summarize(mpg_data = list(mpg), .groups = "drop") %>%
gt() %>%
gt_sparkline(mpg_data)
输出:
现在我希望将上面的代码应用到下面的数据 df
中,这意味着将 type
用作 cyl
,其他 year-month 列用作 mpg-data
:
structure(list(type = c("v1", "v2"), `2017-06` = c(244.955, 9e-04
), `2017-07` = c(244.786, -7e-04), `2017-08` = c(245.519, 0.003
), `2017-09` = c(246.819, 0.0053), `2017-10` = c(246.663, -6e-04
)), class = "data.frame", row.names = c(NA, -2L))
我怎样才能做到这一点?谢谢。
编辑:
data <- melt(df, id = 'type')
data %>%
dplyr::group_by(type) %>%
# must end up with list of data for each row in the input dataframe
dplyr::summarize(values = list(value), .groups = "drop") %>%
gt() %>%
gt_sparkline(values)
输出:
通过添加 value
列编辑数据:
structure(list(type = c("v1", "v2"), `2017-06` = c(244.955, 9e-04
), `2017-07` = c(244.786, -7e-04), `2017-08` = c(245.519, 0.003
), `2017-09` = c(246.819, 0.0053), `2017-10` = c(246.663, -6e-04
), value = c(1.2, 1.6)), class = "data.frame", row.names = c(NA, -2L))
您可以使用 rowwise
并折叠列表中行的所有数据。
library(dplyr)
library(gt)
library(gtExtras)
df %>%
rowwise() %>%
mutate(data = list(c_across(-type))) %>%
select(type, data) %>%
gt() %>%
gt_sparkline(data)
我们可以使用 pmap
来自 purrr
library(gt)
library(gtExtras)
library(dplyr)
library(purrr)
df %>%
transmute(type, data = pmap(across(-type), list)) %>%
gt() %>%
gt_sparkline(data)
输出: