英文:
Create heat map with three factors
问题
我有这样的数据集:
df <- data.frame(Taxa=rep(c("Ants","Birds"),each=4),
Predictor=rep(c("Area","Temp"),times=4),
Method=rep(c(1,1,2,2),times=2),
Importance=c(1,2,2,1,1,2,2,1))
我想在R中制作一个热图,类似于这样的:
有人有建议吗?谢谢!
英文:
I have a dataset like this:
df <- data.frame(Taxa=rep(c("Ants","Birds"),each=4),
Predictor=rep(c("Area","Temp"),times=4),
Method=rep(c(1,1,2,2),times=2),
Importance=c(1,2,2,1,1,2,2,1))
And I would like a heatmap in R like this:
Does anyone has a suggestion? Thanks!
答案1
得分: 2
你可以使用gt
包来着色表格单元格:
library(dplyr)
library(tidyr)
library(gt)
df %>%
pivot_wider(names_from = "Predictor", values_from = "Importance") %>%
group_by(Taxa) %>%
gt() %>%
tab_options(row_group.as_column = TRUE) %>%
data_color(columns = c("Area", "Temp"), colors = c("red", "blue"))
英文:
You can use the gt
package to color table cells:
library(dplyr)
library(tidyr)
library(gt)
df %>%
pivot_wider(names_from = "Predictor", values_from = "Importance") %>%
group_by(Taxa) %>%
gt() %>%
tab_options(row_group.as_column = TRUE) %>%
data_color(columns = c("Area", "Temp"), colors = c("red", "blue"))
答案2
得分: 1
使用ggplot
,我们可以使用facet_wrap()
来按Taxa进行分组显示,与您提供的示例不太相同:
library(ggplot2)
df <- data.frame(
Taxa = rep(c("Ants", "Birds"), each = 4),
Predictor = rep(c("Area", "Temp"), times = 4),
Method = rep(c(1, 1, 2, 2), times = 2),
Importance = c(1, 2, 2, 1, 1, 2, 2, 1)
)
ggplot(data = df, aes(
x = as.character(Method),
y = Predictor,
fill = Importance
)) + geom_tile() +
facet_wrap( ~ Taxa) + xlab("Method")
英文:
Not quite like the example you provided, but using ggplot
we can use facet_wrap()
to display the grouping by Taxa:
library(ggplot2)
df <- data.frame(
Taxa = rep(c("Ants", "Birds"), each = 4),
Predictor = rep(c("Area", "Temp"), times = 4),
Method = rep(c(1, 1, 2, 2), times = 2),
Importance = c(1, 2, 2, 1, 1, 2, 2, 1)
)
ggplot(data = df, aes(
x = as.character(Method),
y = Predictor,
fill = Importance
)) + geom_tile() +
facet_wrap( ~ Taxa) + xlab("Method")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论