0

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,
  • Please follow: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Divi Jul 19 '16 at 16:09
  • can u help me as to where i am going wrong>? –  Jul 19 '16 at 16:18
  • Please provide a reproducible example. – Divi Jul 19 '16 at 16:19
  • 2
    You are only using the third column of `df` for plotting in `stat_density2d`. Try using the whole thing: `data = df` instead of `data = df[,3]`. – aosmith Jul 19 '16 at 16:27
  • thanks aosmith. But the output does not plot temperature correctly. It shows only the bands randomly –  Jul 19 '16 at 16:51
  • can someone please help ? –  Jul 19 '16 at 17:30
  • The error is caused by `data = df[,3]`. If you have an additional problem over that error you'll need to clarify what it is. If it involves using a third variable on 2d density plots, see if [this answer](http://stackoverflow.com/a/18293743/2461552) helps you make any progress. – aosmith Jul 19 '16 at 18:40
  • I want to plot the latitude on y axis and longitude on x axis. Then after getting the point on the base map(google map), I want to plot the temperature at that location. That is I want to plot choropleth map there. –  Jul 19 '16 at 18:58

0 Answers0