I already posted a question about heatmaps a few days ago: R heatmap: assign colors to values
Answers already helped me a lot with my issues (thanks @Pedro Alencar), so this code works:
# Library
library(ggplot2)
library(plyr)
set.seed(10)
# Dummy data
x <- LETTERS[1:20]
y <- paste0("var", seq(1,20))
data <- expand.grid(X=x, Y=y)
data$Z <- runif(400, -1, 2)
print (data)
data$Z <- factor(round(data$Z))
data$Z <- revalue(data$Z, c('-1'='negative'))
data$Z <- revalue(data$Z, c('0' = 'no'))
data$Z <- revalue(data$Z, c('1' = 'yes'))
data$Z <- revalue(data$Z, c('2' = 'other'))
# Heatmap
ggplot(data, aes(X, Y, fill= Z)) +
geom_tile() +
theme (axis.title.y = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank()) +
scale_fill_manual(values = c('#DCDCDC', '#888888', '#17334E', '#3773A4'), name = 'Z')
Now as my data structure is different I would ask again for help with this topic. This is my data structure now:
I now want to apply the code of @Pedro Alencar to an exising dataframe which has the following structure:
DF.heatmap <- data.frame(
round(runif(100, -1, 2)),
round(runif(100, -1, 2)),
round(runif(100, -1, 2)),
round(runif(100, -1, 2)),
round(runif(100, -1, 2)))
colnames (DF.heatmap) <- c("col1", "col2", "col3", "col4", "col5")
DF.heatmap[DF.heatmap == "-1"] <- "negative"
DF.heatmap[DF.heatmap == "0"] <- "no"
DF.heatmap[DF.heatmap == "1"] <- "yes"
DF.heatmap[DF.heatmap == "2"] <- "other"
This dataframe consists of the columns "col1" to "col5" and has 100 rows with values from -1 to 2 in each column and "negative" to "other" after replacement, respectively. Now I would like to show the values of col1 to col5 in the columns of the heatmap and every row in the dataframe is a row in the heatmap.
Sorry, but I'm really new to R and I don't know how to apply the dataframe DF.heatmap to the ggplot statement.
Thank everyone for help!