Reputation:
I want to plot population data. The data is raster data. The map should have red areas where less than 1 person lives, and another color for areas with more than one person. If I just use the plot()
function I cannot achieve it. My data comes from: http://sedac.ciesin.columbia.edu/data/set/gpw-v4-population-count-rev10
Any idea how to solve the problem?
Upvotes: 1
Views: 1224
Reputation: 9809
There are many similar questions like that and a lot of answers, but maybe these 2 options might help.
library(raster)
## Create random raster
spg <- data.frame( x = rep( 0:1, each=2 ),
y = rep( 0:1, 2),
l = c(0.8,1,1.1,100));
coordinates(spg) <- ~ x + y
gridded(spg) <- TRUE
rasterDF <- raster(spg)
## Assign values, based on your condition
values(rasterDF) <- as.numeric(values(rasterDF) >= 1)
## Create a Color Function
cpal <- colorRampPalette(c("red", "blue"))
## Plot with raster-package
plot(rasterDF, col=cpal(2))
## Plot with rasterVis package
library(rasterVis)
r2 <- ratify(rasterDF)
levelplot(r2, col.regions=cpal, att='ID')
Upvotes: 2
Reputation: 503
How about this:
library(raster)
myColorRamp <- colorRampPalette(c("red", "blue"))
popRaster <- raster("path/to/my/raster")
values(popRaster) <- as.numeric(values(popRaster) >= 1)
plot(popRaster, col=myColorRamp(2))
You need to install the raster
package, which I believe has the sp
package as a dependency (and possibly rgdal
).
Upvotes: 2