r - Plotting polygons and lines of lat and lon on the map -
so have following colums in dataset:
lat =c(10, 20-30, 40, 60-70) lon=c(15, 35-45, 50-60, 70) num=c(4,5,2,3) data= data.frame('dec_lat'=lat, 'dec_lon'=lon,'value' =num)
i have these lat/lon coordinates want plot on map. however, of them points, polygons , lines.
in addition, have 5 5 dehree grid on map, want fill squares based on number num column.
when lat , lon in form of point- easy do. not sure how fill in squares of grid line or polygon overlap.
i appriciate ideas on how that. using grid , map following example : http://www.iobis.org/manual/processing/
everything works points not sure how adjust polygons , lines
#create quick world map world <- map_data("world") #this creates quick , dirty world map - playing around themes, aesthetics, , device dimensions recommended! worldmap <- ggplot(world, aes(x=long, y=lat)) + geom_polygon(aes(group=group)) + scale_y_continuous(breaks = (-2:2) * 30) + scale_x_continuous(breaks = (-4:4) * 45) + theme(panel.background = element_rect(fill = "steelblue")) + # "steelblue" coord_equal() #it may preferable in these cases plot data on grid. here, create 5x5 degree global grid: global_grid <- raster(nrows = 180/5, ncol = 360/5, xmn = -180, xmx = 180, ymn = -90, ymx = 90) #get longitude , latitude: allspp_obis <- data[!(is.na((data$dec_lat))) | !(is.na((data$dec_lon))) ,] lonlat_sp <- cbind(y = allspp_obis$dec_lat, x = allspp_obis$dec_lon) #and sum these on grid: gridded_occs <- rasterize(x = lonlat_sp, y = global_grid, fun = "count") #now add occurrence points: occ_map <- worldmap + geom_point(data = (data), aes(x = latlon$dec_lat, y = latlon$dec_lon), colour = "darkorange", shape = 21, alpha = 2/3) #to render these using ggplot, i’ve adapted code here. first, turn gridded data dataframe of points, , name variables sensibly: obis.p <- data.frame(rastertopoints(gridded_occs)) names(obis.p) <- c("longitude", "latitude", "obis") #then add these using geom_raster worldmap generated previously: (worldmap + geom_raster(data = obis.p, aes(x = longitude, y = latitude, fill = obis)) + scale_fill_gradient(low = "white", high = "darkorange") )
Comments
Post a Comment