[英]Heatmap with categorical variables in R
I am trying to create a heatmap with long form data to assess the correlation between membership of individual groups on specific outcome measure scores.我正在尝试使用长格式数据创建一个热图,以评估各个组的成员资格与特定结果测量分数之间的相关性。 I have converted the data to long format as shown.如图所示,我已将数据转换为长格式。 Some individuals may be members of more than 1 group.有些人可能是超过 1 个组的成员。
df1 <- data.frame(ID = c(1, 2, 3, 4, 5),
Group1= c(1, 1, 0, 1, 0),
Group2 = c(1, 0, 1, 0, 0),
Measurement = c("Pain", "Pain", "Fatigue", "Stiffness", "Fatigue"),
Score = c(2, 3, 3, 2, 3))
df1
ID Group1 Group2 Measurement Score
1 1 1 1 Pain 2
2 2 1 0 Pain 3
3 3 0 1 Fatigue 3
4 4 1 0 Stiffness 2
5 5 0 0 Fatigue 3
I am sure this is a very basic question so my apologies.我确信这是一个非常基本的问题,所以我很抱歉。 I would like to create a heatmap-like plot in ggplot with a colour scale representing the scores and x axis= Group memberships, y-axis = Measurements我想在 ggplot 中创建一个类似于热图的 plot ,其色阶代表分数,x 轴 = 组成员资格,y 轴 = 测量
Running the code below yields the following:运行下面的代码会产生以下结果:
fig1 <- ggplot(df1, aes(x = c("Group1", "Group2"),
y = "Measurement",
fill = "Score")) + geom_tile()
Error: Aesthetics must be either length 1 or the same as the data (5): x错误:美学必须是长度 1 或与数据 (5) 相同:x
Is there a way of having a list of categorical variables on the y and x axes as described?有没有一种方法可以在 y 轴和 x 轴上列出分类变量的列表?
Thanks谢谢
There are two issues with your code:您的代码有两个问题:
"
around column names which is the reason why you get the error message.您在列名周围使用引号"
,这就是您收到错误消息的原因。c(Group1, Group2)
will not work.您不能 map 在一个美学上的两列,即即使删除引号c(Group1, Group2)
也不起作用。 Instead combine both variables into one for which I use dplyr::case_when
.而是将这两个变量合并为一个,我使用dplyr::case_when
。library(ggplot2)
library(dplyr)
df1 <- df1 %>%
mutate(Group = dplyr::case_when(
Group1 == 1 & Group2 == 1 ~ "Group 1 & 2",
Group1 == 1 ~ "Group 1",
Group2 == 1 ~ "Group 2",
TRUE ~ "None"
))
ggplot(df1, aes(
x = Group,
y = Measurement,
fill = Score
)) +
geom_tile()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.