How to create heatmap from multiple data frames

A75awh :

I'm quite new to R and I'm stuck on how to create a heatmap from multiple data frames in a list.

There are 3 columns in the each data frame: X location, Y location, PatchStatus

The first data frame looks like:

listofdfs <- list() #list of dataframes
listofdfs[1]

  allPoints.xLocs allPoints.yLocs allPoints.patchStatus
1       67.30330212       87.857495                     0
2       69.60800088       77.959314                     0
3       74.63313295       93.059260                     0
4       92.59099136       77.732215                     1
5       18.05288289       61.200910                     1
6       55.83499856       50.993785                     0
7       12.15664148       58.220179                     1
8       41.50413859       92.529054                     0
9       83.08209025       24.567501                     0
10      53.50615149       46.339927                     0

...and so on up to 100 points

Each data frame will have the same X and Y locations, however Patch Status will be different (can be 0 or 1).

My goal is to create a heatmap combining all the data frames (I intend to have 10-15 data frames) showing the patches that are more susceptible to having a status of "1". Any help would be greatly appreciated. Thanks.

Sathish :

Data:

df <- read.table(text = "allPoints.xLocs allPoints.yLocs allPoints.patchStatus
1       67.30330212       87.857495                     0
                 2       69.60800088       77.959314                     0
                 3       74.63313295       93.059260                     0
                 4       92.59099136       77.732215                     1
                 5       18.05288289       61.200910                     1
                 6       55.83499856       50.993785                     0
                 7       12.15664148       58.220179                     1
                 8       41.50413859       92.529054                     0
                 9       83.08209025       24.567501                     0
                 10      53.50615149       46.339927                     0", header = TRUE, stringsAsFactors = FALSE)

listofdfs <- list(df, df)

Code:

library('data.table')
listofdfs <- lapply(seq_len(length(listofdfs)), function(i){
  x <- listofdfs[[i]]
  # assign id and combine x and y coordinates
  setDT(x)[, `:=` ( id = i, coords = paste0(allPoints.xLocs, ",", allPoints.yLocs)) ]
} )

# combine list into a data table.
df2 <- rbindlist(l = listofdfs)

Plot

library('ggplot2')
ggplot( data = df2, mapping = aes( x = coords, y = factor(id) ) ) +  # draw heatmap
  geom_tile( aes( fill = factor(allPoints.patchStatus) ),  colour = "white") +
  coord_flip() + 
  scale_fill_discrete(name = "Patch Status") +
  labs( x = "Coordinates", y = "Data Frame Number")

Graph:

enter image description here

You can loop through the list of data frames and create heatmap for each dataframe. Below, I am showing how to get a heatmap for one data frame.

Plot-2

ggplot( data = df, mapping = aes( x = factor(allPoints.xLocs), y = factor(allPoints.yLocs) ) ) + 
  geom_tile( aes( fill = factor(allPoints.patchStatus) ),  colour = "white") +
  scale_fill_discrete(name = "Patch Status") +
  labs( x = "X-Coordinate", y = "Y-Coordinate") +
  theme_bw() + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Graph-2

enter image description here

Plot-3 df data is used from above - see Data section at the top.

library('data.table')
listofdfs <- list(df, df)
df2 <- rbindlist(l = listofdfs)
df2 <- df2[, .(sum_patch = sum(allPoints.patchStatus)), by = .(allPoints.xLocs, allPoints.yLocs)]

library('ggplot2')
ggplot( data = df2, mapping = aes( x = factor(allPoints.xLocs), y = factor(allPoints.yLocs) ) ) + 
  geom_tile( aes( fill = sum_patch ),  colour = "white") +
  labs( x = "X-Coordinate", y = "Y-Coordinate") +
  theme_bw() + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Graph-3:

enter image description here

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=383481&siteId=1