Как создать тепловую карту из нескольких фреймов данных - PullRequest
2 голосов
/ 01 апреля 2020

Я довольно новичок в R и застрял на том, как создать тепловую карту из нескольких фреймов данных в списке.

В каждом фрейме данных есть 3 столбца: 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 точек

Каждый кадр данных будет иметь одинаковые X и Y местоположения, однако Patch Status будет другим (может быть 0 или 1).

Моя цель - создать тепловую карту, объединяющую все фреймы данных (у меня будет 10-15 фреймов данных), показывающие патчи, которые более подвержены состоянию «1». Любая помощь будет принята с благодарностью. Спасибо.

1 Ответ

1 голос
/ 01 апреля 2020

Данные:

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

График:

enter image description here

Вы можете l oop просмотреть список фреймов данных и создать тепловую карту для каждого фрейма данных. , Ниже я показываю, как получить тепловую карту для одного фрейма данных.

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 вверху.

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:

enter image description here

...