基于R中的值创建热图

基于R中的值创建热图,r,heatmap,ggmap,R,Heatmap,Ggmap,我尝试根据数值生成热图。 这是我的数据集,它由三个变量组成:纬度(Lat)、经度(long)和值。 我浏览了相关帖子,发现这很有用: 我在那篇文章中复制了代码,我的代码如下所示: # import data and libaries library(ggplot2) library(ggmap) Yunan<-read.csv("C:\\Program Files\\RStudio\\data\\pb_sp\\sample_data.csv", header = TRUE) # c

我尝试根据数值生成热图。 这是我的数据集,它由三个变量组成:纬度(Lat)、经度(long)和值。

我浏览了相关帖子,发现这很有用:

我在那篇文章中复制了代码,我的代码如下所示:

# import data and libaries 
library(ggplot2)
library(ggmap)
Yunan<-read.csv("C:\\Program Files\\RStudio\\data\\pb_sp\\sample_data.csv", header = TRUE)

# call the map to see point distribution
Yunan_map<-get_map(location="yunan",zoom=6,maptype="terrain",scale=2)
ggmap(Yunan_map)+geom_point(data=Yunan,aes(x=Yunan$Lon,y=Yunan$Lat,fill="red",alpha=0.3,size=0.05,shape=21))+scale_shape_identity()


# 1.  generate bins for x, y coordinates (unit=decimal degree)
xbreaks <- seq(floor(min(Yunan$Lat,na.rm=TRUE)), ceiling(max(Yunan$Lat,na.rm=TRUE)), by = 0.5)
ybreaks <- seq(floor(min(Yunan$Lon,na.rm=TRUE)), ceiling(max(Yunan$Lon,na.rm=TRUE)), by = 0.5)

# 2.  allocate the data points into the bins
Yunan$latbin <- xbreaks[cut(Yunan$Lat, breaks = xbreaks, labels=F)]
Yunan$longbin <- ybreaks[cut(Yunan$Lon, breaks = ybreaks, labels=F)]

# 3.  summarise the data for each bin (use the median)
datamat <- Yunan[, list(Value= median(Value)), 
                 by = c("latbin", "longbin" )]

# 4. Merge the summarised data with all possible x, y coordinate combinations to get 
# a value for every bin
datamat <- merge(setDT(expand.grid(latbin = xbreaks, longbin = ybreaks)), datamat, 
                 by = c("latbin", "longbin"), all.x = TRUE, all.y = FALSE)

# 5. Fill up the empty bins 0 to smooth the contour plot
datamat[is.na(Value), ]$Value <- 0

# 6. Plot the contours
ggmap(Yunan_map,extent ="device") +
  stat_contour(data = datamat, aes(x = longbin, y = latbin, z = Value, 
                                   fill = ..level.., alpha = ..level..), geom = 'polygon', binwidth = 30) +
  scale_fill_gradient(name = "Value", low = "green", high = "red") +
  guides(alpha = FALSE)
#导入数据和库
图书馆(GG2)
图书馆(ggmap)

YunanIt是因为包
data.table
您所指的示例使用了itI,我安装了此包并再次运行代码,但不幸的是,我仍然出现了相同的错误。
[.data.frame
(Yunan,list(Value=median(Value)),by=c(“latbin”),:未使用的参数(by=c(“latbin”,“longbin”))这一步使用的是包
数据,效果很好。谢谢。现在我只需要知道如何调整配色方案。默认设置为等距离的箱子,这使我很难分辨不同的值。这是因为包
数据。表
您所指的示例使用了itI,我已经安装了此包,并且再次运行代码,但不幸的是,我仍然出现相同的错误。
[.data.frame
(Yunan,list(Value=median(Value)),by=c(“latbin”),:未使用的参数(by=c(“latbin”,“longbin”))这一步使用的包
数据效果很好。谢谢。现在我只需要知道如何调整配色方案。默认设置为等距离的箱子,这使我很难分辨不同的值。