[R] help using ggplot for contour plot

Jatin Kala jatin.kala.jk at gmail.com
Fri Dec 16 07:03:36 CET 2016


Hi R community,
I am trying to do a contour plot of (longitude, latitude, concentration).
This is easy enough using filled.contour.
However, i am trying to do the same contour plot using ggplot, and 
overlaying the plot on google-map.
The script and data being read is attached.
  test_filled_contour_plot.png is what contour.filled gives me.
GF_test.png is the output from using ggplot. Something is going wrong, 
but i can't figure out what, and it's doing my head-in....
Any help would be greatly appreciated.
Cheers,
Jatin

-------------- next part --------------

rm(list = ls())
library(ggmap)
library(rgdal)

#########################
load("ALL_HOURLY_NOx_Processed")
# need to convert lat, lon, concnetration into a data-frame before plotting
# convert 2D conc array into 1d vector
utmcoor<-SpatialPoints(cbind(x,y), proj4string=CRS("+proj=utm +zone=50 +ellps=WGS84 +south"))
longlatcoor<-spTransform(utmcoor,CRS("+proj=longlat"))
lon <-longlatcoor at coords[,1]
lat <-longlatcoor at coords[,2]

# quick plot using filled.contour so we know what the plot should look like
png("test_filled_countour_plot.png")
filled.contour(x=lon,y=lat,max_conc)
dev.off()

max_conc_vector <- as.vector(max_conc)
# make equivalent 1d lat and lon arrays
dim_array <- dim(max_conc)
lat2d <- matrix(data=NA,nrow=dim_array[1],ncol=dim_array[2])
lon2d <- lat2d
for (i in 1:dim_array[1]) {
  lon2d[,i] <- lon
}
for (i in 1:dim_array[2]) {
  lat2d[i,] <- lat
}

# this does a google map plot only
Freo = get_map(location = c(min(lon), min(lat), max(lon), max(lat)), source = "osm", zoom = 12)

# now make data frame, lat, lon, concentration
lat1d <- as.vector(lat2d)
lon1d <- as.vector(lon2d)
data_plot <- data.frame(lon1d,lat1d,max_conc_vector)

############
# Plotting concs on map
## Data_plot is not the actual data I want, my concs need to be in a data frame in order to use code below
FreoMap <- ggmap(Freo, extent = "panel" , legend = "topright")
#FreoMap + stat_density2d(aes(x = lon1d, y = lat1d, fill = max_conc_vector, alpha = 0.5),size = 2, bins = 6, data = data_plot, geom = "polygon")
FreoMap + stat_density2d(aes(x = lon1d, y = lat1d, fill = max_conc_vector, alpha = 0.5), data = data_plot, geom = "polygon")
ggsave("GF_test.png")
-------------- next part --------------
A non-text attachment was scrubbed...
Name: GF_test.png
Type: image/png
Size: 1428218 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20161216/3515d374/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_filled_countour_plot.png
Type: image/png
Size: 58183 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20161216/3515d374/attachment-0003.png>


More information about the R-help mailing list