我对R还是很陌生,我一直坚持如何从列表中的多个数据帧创建热图。
每个数据帧中有3列:X位置,Y位置,PatchStatus
第一个数据帧如下:
>>> pd.to_datetime(df['dt'], format = '%d-%m-%Y')
Traceback (most recent call last):
File "/home/vishnudev/anaconda3/envs/sumyag/lib/python3.7/site-packages/pandas/core/tools/datetimes.py", line 448, in _convert_listlike_datetimes
values, tz = conversion.datetime_to_datetime64(arg)
File "pandas/_libs/tslibs/conversion.pyx", line 200, in pandas._libs.tslibs.conversion.datetime_to_datetime64
TypeError: Unrecognized value type: <class 'str'>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/vishnudev/anaconda3/envs/sumyag/lib/python3.7/site-packages/pandas/util/_decorators.py", line 208, in wrapper
return func(*args, **kwargs)
File "/home/vishnudev/anaconda3/envs/sumyag/lib/python3.7/site-packages/pandas/core/tools/datetimes.py", line 778, in to_datetime
values = convert_listlike(arg._values, True, format)
File "/home/vishnudev/anaconda3/envs/sumyag/lib/python3.7/site-packages/pandas/core/tools/datetimes.py", line 451, in _convert_listlike_datetimes
raise e
File "/home/vishnudev/anaconda3/envs/sumyag/lib/python3.7/site-packages/pandas/core/tools/datetimes.py", line 416, in _convert_listlike_datetimes
arg, format, exact=exact, errors=errors
File "pandas/_libs/tslibs/strptime.pyx", line 142, in pandas._libs.tslibs.strptime.array_strptime
ValueError: time data '3-24-2020' does not match format '%d-%m-%Y' (match)
...以此类推,最多100点
每个数据帧将具有相同的X和Y位置,但是补丁状态将有所不同(可以为0或1)。
我的目标是创建一个结合了所有数据帧(我打算有10-15个数据帧)的热图,这些数据帧显示了更容易出现“ 1”状态的补丁。任何帮助将不胜感激。谢谢。
答案 0 :(得分:1)
数据:
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")
图形:
您可以遍历数据帧列表,并为每个数据帧创建热图。下面,我展示了如何获取一个数据帧的热图。
图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: