Reputation: 31
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.
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
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
Is there a way of having a list of categorical variables on the y and x axes as described?
Thanks
Upvotes: 1
Views: 837
Reputation: 125537
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. Instead combine both variables into one for which I use 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()
Upvotes: 1