创建三因素热力图。

huangapple go评论86阅读模式
英文:

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 &lt;- data.frame(Taxa=rep(c(&quot;Ants&quot;,&quot;Birds&quot;),each=4),
                 Predictor=rep(c(&quot;Area&quot;,&quot;Temp&quot;),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 %&gt;% 
  pivot_wider(names_from = &quot;Predictor&quot;, values_from = &quot;Importance&quot;) %&gt;% 
  group_by(Taxa) %&gt;% 
  gt() %&gt;% 
  tab_options(row_group.as_column = TRUE) %&gt;% 
  data_color(columns = c(&quot;Area&quot;, &quot;Temp&quot;), colors = c(&quot;red&quot;, &quot;blue&quot;))

创建三因素热力图。

答案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 &lt;- data.frame(
  Taxa = rep(c(&quot;Ants&quot;, &quot;Birds&quot;), each = 4),
  Predictor = rep(c(&quot;Area&quot;, &quot;Temp&quot;), 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(&quot;Method&quot;)


创建三因素热力图。

huangapple
  • 本文由 发表于 2023年6月15日 18:39:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定