I am reading a csv file and I want to plot places using the longitude and latitude mentioned in the file. The temperature at those places is also mentioned in the csv and I want to draw a heatmap using that temperature value. I have read the csv file in variable st and created a dataframe extracting latitude,longitude and temperature. I was plotting the map and created the heatmap. I have passed the temperature as the data in stat_density2d. I get the error "ggplot2 doesn't know how to deal with data of class integer" Following is the code:
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")
The sample csv file is shown below:
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,