I have a dataframe called melted_cormat, and I am trying to use it to plot a heatmap in ggplot. I also have a list of new_names = c(a,b,c) that I would like to make bold on the heatmap. I managed to plot the heatmap, but I have been having issues making bold the list of names that I have. Since the new_names that I want to make bold appear more than once in the Var1 and Var2 columns I am plotting, I used the list of new_names to create additional columns on the melted_cormat namely "names_to_consider" and "names_to_color" to try and use these to specify the names that I want to bold or color. However this is not working, the best I managed to do was make bold everything, or bold one line and skip the other, but not only the three names that I want, eg. this heatmap2 where a, b and c appear in bold. I have included my code below. I have looked up at other pages that helped with the code to specify certain names to bold (e.g Mark certain axis text in bold), the only difference now is that the names appear in a matrix. Thank you in advance for all your help.
new_names = c("a","b","c")
#I tried to narrow down the axis labels to consider, and defined them as those points in the matrix where the Var label is the same as the Var2 label
melted_cormat = melted_cormat %>%
mutate(names_to_consider=case_when(Var1==Var2 ~ "consider", Var1!=Var2 ~ "no"))
#among those that I narrowed down, I then used my list of names to create a column "names_to_color", and I managed to enter yes on the three names that I wanted.
#among consider, change the new names to 'yes', and everything else to 'no'
melted_cormat = melted_cormat %>%
mutate(names_to_color=case_when(names_to_consider == "consider" & Var1 %in% new_names ~ "yes",
names_to_consider == "consider" & !(Var1 %in% new_names) ~ "no",
names_to_consider == "no" ~ "no"))
melted_cormat$names_to_color = as.factor(melted_cormat$names_to_color)
#vec_fontface <- ifelse(levels(melted_cormat$names_to_color[melted_cormat$Var2])=="yes","bold","plain")
#plot
ggplot(data = melted_cormat, aes(x=Var1, y=Var2, fill = value))+
geom_tile(color = "white")+
geom_text(data=subset(melted_cormat,value < 200), aes(label = value), size=2) +
scale_fill_gradient2(low = "white", high = "violet", limit = c(0,maximum), space = "Lab", name="key") +
theme_classic()+
theme(plot.title = element_text(color="black", size=10, face="bold.italic"),axis.title.x=element_blank(), axis.title.y=element_blank(), axis.text.x = element_text(angle = 45, vjust = 1, size = 8, hjust = 1)) +
coord_fixed() +
ggtitle("myplot") +
scale_y_discrete(labels=c("a"=expression(bold(`a`)), "b"=expression(bold(`b`)),
"c"=expression(bold(`c`)), parse=TRUE))