我正在阅读csv文件,我想使用文件中提到的经度和纬度来绘制地点。这些地方的温度也在csv中提到,我想用该温度值绘制热图。 我已经在变量st中读取了csv文件,并创建了一个提取纬度,经度和温度的数据帧。 我正在绘制地图并创建热图。我已将温度作为stat_density2d中的数据传递。我得到错误" ggplot2不知道如何处理类整数的数据" 以下是代码:
library(ggmap)
st <- read.csv("C:/Python27/csv/daily.csv")
longitude <- st$longitude
latitude <- st$latitude
temperature <- st$temperature
df <- data.frame(longitude,latitude,temperature)
houston <- get_map(location = c(lon = st$longitude[1], lat = st$latitude[1]), zoom = 6)
ggmap(houston)
ggmap(houston) +
# make the heatmap
stat_density2d(aes(x = longitude,
y = latitude,
fill = ..level.., # value corresponding to discretized density estimates
alpha = ..level..),
bins = 2, # number of bands
data <- df[,3] ,
geom = "polygon") + coord_fixed(xlim = c(-119,-121),ylim = c(35,37),ratio=1/1)+
scale_fill_gradient(low = "yellow", high = "red") +
scale_alpha(range = c(.25, .55)) +
theme(legend.position="none")
示例csv文件如下所示:
Name,latitude,longitude,temperature,
Five Points,36.34,-120.11,
Shafter/USDA,35.53,-119.28,
Firebaugh/Telles,36.85,-120.59,
Stratford,36.16,-119.85,67,
Kettleman,35.87,-119.89,50,
Parlier,36.60,-119.50,73,
Blackwells Corner,35.65,-119.96,70,
Los Banos,37.01,-120.76,75,
Fresno State,36.82,-119.74,0,
Westlands,36.63,-120.38,69,