Ant*_*tin 0 datatable r data-visualization heatmap
我有以下数据:
\nlibrary(data.table)\nDT <- data.table(\n Pays = c("Austria", "Belgium", "Brazil", "Bulgaria", \n "Canada", "China", "Croatia", "Cyprus", "Czechia", "Denmark"), \n `Nouveaux cas` = c("212.2", "136.0", "143.5", "258.7", "122.8", \n "0.0", "615.0", "299.0", "327.7", "314.5"), \n `Nouveaux d\xc3\xa9c\xc3\xa8s` = c("53.8", "35.4", "14.9", "89.7", "14.2", "0.0", "79.0", "14.4", "47.3", "6.9"), \n `\xc3\x89volution du nombre de cas` = c("-21.5%", "2.8%", "5.6%", "-5.2%", "4.1%", "-0.9%", "5.5%", "34.3%", "36.7%", "73.8%"), \n `\xc3\x89volution du nombre de d\xc3\xa9c\xc3\xa8s` = c("-20.1%", "-20.4%", "10.5%", "-8.5%", "24.1%", "NaN%", "2.9%", "63.6%", "-5.4%", "3.6%"))\n\n> DT\n Pays Nouveaux cas Nouveaux d\xc3\xa9c\xc3\xa8s \xc3\x89volution du nombre de cas \xc3\x89volution du nombre de d\xc3\xa9c\xc3\xa8s\n 1: Austria 212.2 53.8 -21.5% -20.1%\n 2: Belgium 136.0 35.4 2.8% -20.4%\n 3: Brazil 143.5 14.9 5.6% 10.5%\n 4: Bulgaria 258.7 89.7 -5.2% -8.5%\n 5: Canada 122.8 14.2 4.1% 24.1%\n 6: China 0.0 0.0 -0.9% NaN%\n 7: Croatia 615.0 79.0 5.5% 2.9%\n 8: Cyprus 299.0 14.4 34.3% 63.6%\n 9: Czechia 327.7 47.3 36.7% -5.4%\n10: Denmark 314.5 6.9 73.8% 3.6%\nRun Code Online (Sandbox Code Playgroud)\n我想知道获取对象并将其插入到 Word 文件中的最佳方法,即该表每列一个热图。我可以在 Excel 中导出该表并使用条件格式来获得所需的输出:
\n\n现在我不确定在 R 中做到这一点的最佳方法是什么。我的快速搜索让我得到了:
\n\n这是一种方法gt。
gt::data_color仅当数字数据实际上是数字时才真正适用于数字数据,因此我将数据中的百分比列转换回数字。我使用它是readr::parse_number因为它工作起来很容易,但如果你愿意,你也可以手动完成。
scales::col_numeric可以轻松定义色标。更改domain =参数以修改范围。
library(gt)
library(scales)
library(readr)
DT <- DT[,lapply(.SD,readr::parse_number),by = Pays]
DT %>%
gt() %>%
data_color(columns = 2:3,
colors = col_numeric(palette = c("white","red"),
domain = c(0,650))) %>%
data_color(columns = 4:5,
colors = col_numeric(palette = c("green","yellow","red"),
domain = c(-30,80))) %>%
fmt_percent(columns = 4:5, scale_values = FALSE, decimals = 1)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4107 次 |
| 最近记录: |