如何从多个数据帧创建热图 [英] How to create heatmap from multiple data frames

查看:66
本文介绍了如何从多个数据帧创建热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对R还是很陌生,我一直坚持如何从列表中的多个数据帧创建热图。

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

每个数据框:X位置,Y位置,PatchStatus

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

第一个数据框如下:

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

...依此类推,最高可达100分

...and so on up to 100 points

每个数据帧将具有相同的X和Y位置,但是补丁状态将不同(可以为0或1)。

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

我的目标是创建一个结合了所有数据帧(我打算有10-15个数据帧)的热图,这些数据帧显示了更容易出现以下状态的补丁: 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.

推荐答案

数据:

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)

代码:

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)

>

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")

图形:

您可以遍历数据帧列表,并为每个数据帧创建热图。下面,我演示如何获取一个数据帧的热图。

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.

图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))

图2

Plot-3
df 数据用于上面-请参见顶部的数据部分。

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))

图3:

这篇关于如何从多个数据帧创建热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆