Reputation: 435
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!
Upvotes: 0
Views: 808
Reputation: 1763
First I would transform your dataframe as you needed with your precedent code, and then using your ggplot code to plot the heatmap :
library(tidyr)
library(plyr)
#Create an ID column to keep the row line number
DF.heatmap$ID = rownames(DF.heatmap)
#Reformat your dataframe with only 3 columns
DF = pivot_longer(DF.heatmap,cols=1:5,names_to="Col",values_to="Value")
colnames(DF)=c("X","Y","Z")
#Your ggplot code
DF$Z <- factor(round(DF$Z))
DF$Z <- revalue(DF$Z, c('-1'='negative'))
DF$Z <- revalue(DF$Z, c('0' = 'no'))
DF$Z <- revalue(DF$Z, c('1' = 'yes'))
DF$Z <- revalue(DF$Z, c('2' = 'other'))
#Reorder the rows in descending order
DF$X=as.numeric(DF$X)
DF$X=reorder(DF$X,desc(DF$X))
ggplot(DF, aes(Y, X, fill= Z)) +
geom_tile(color = "white",
lwd = 0.5,
linetype = 1)+
scale_fill_manual(values = c('red', 'green', 'yellow', 'blue'), name = 'Z')
After you can arrange your plot to make it readable, as there is here a lot of Y-axis labels
Upvotes: 1