複数のデータフレームからヒートマップを作成する方法

A75awh:

私はRに非常に新たなんだと私は、リスト内の複数のデータフレームからヒートマップを作成する方法についてこだわっています。

X位置、Y位置、PATCHSTATUS:各データフレーム内の3つの列が存在します

最初のデータフレームのルックスが好き:

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ポイントに

各データフレームは、しかし、パッチのステータス(0又は1であることができる)異なるであろう、同じX及びY位置を有することになります。

私の目標は、「1」の状態を有することに影響されやすいのパッチを示すすべてのデータフレーム(私は10-15のデータフレームを持っているつもり)を組み合わせヒートマップを作成することです。任意の助けいただければ幸いです。ありがとうございました。

サティッシュ:

データ:

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

グラフ:

ここでは、画像の説明を入力します。

あなたはできるデータフレームのリストをループし、各データフレームのためのヒートマップを作成します。以下は、私は1つのデータフレームのためのヒートマップを取得する方法を示しています。

プロット-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

ここでは、画像の説明を入力します。

プロット-3 dfを参照-データが上から使用されているデータの先頭にセクションを。

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:

ここでは、画像の説明を入力します。

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=384528&siteId=1