I'm trying to create a heatmap from the following matrix (bnc) :
Which would look basically like this (here it's another frequency matrix) :
I'm at the very first stage where I'm trying to create the heatmap, and have not yet figured out details about the colour scale and the blacking out of NA values.
When I run the heatmap function :
heatmap(bnc)
I get the following error :
Error in hclustfun(distfun(if (symm) x else t(x))) :
NA/NaN/Inf in call to external function (argument 10)
Could someone help me figure out what's wrong here?
Best
Cameron
EDIT : here is the dput() of my dataframe:
structure(c(NA, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 2, NA,
0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, NA, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 2, 8, 1, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6, 3, 0, 0, NA, 2, 0, 0, 0, 0, 0, 0, 0, 1, 2, 8, 0, 0, 1, NA,
0, 1, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 0, 0, NA, 0, 0, 0, 0, 0,
0, 0, 1, 6, 0, 0, 1, 0, 0, NA, 0, 0, 0, 1, 0, 0, 2, 1, 0, 0,
0, 1, 0, 0, NA, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, NA,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA, 0, 0, 0, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA), .Dim = c(14L, 14L), .Dimnames = list(
c("WILL_", "WOULD_", "MAY_", "MIGHT_", "CAN_", "COULD_",
"SHALL_", "SHOULD_", "MUST_", "OUGHT TO_", "USED TO_", "HAVE TO_",
"GOING TO_", "BE ABLE TO_"), c("_WILL", "_WOULD", "_MAY",
"_MIGHT", "_CAN", "_COULD", "_SHALL", "_SHOULD", "_MUST",
"_OUGHT TO", "_USED TO", "_HAVE TO", "_GOING TO", "_BE ABLE TO"
)))
PS: I would like my heatmap to show the columns which are entirely made of NA values.
You can try with ggplot2
reshape2::melt(df, value.name = "Freq") %>%
mutate(label = ifelse(is.na(Freq), "NA", as.character(Freq))) %>%
ggplot(aes(Var1, Var2)) +
geom_tile(aes(fill = Freq), color = "white") +
geom_text(aes(label = label), color = "white") +
scale_fill_gradient(low = "blue", high = "red", na.value = "black") +
scale_x_discrete(NULL, expand = c(0, 0)) +
scale_y_discrete(NULL, expand = c(0, 0)) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))