Reputation: 7
I'm plotting the plot below using R's plot function. This plot represented in the form of different point values, was obtained from different locations along the coast of Brazil in South America. The Colour points in a plot differ depending on a vector of values.
To better represent the South America coast in this plot, I would like to insert a georeferenced file of image below showing the shoreline. I want that the points stay on the shoreline for better representation. It's possible to do this in R?
I already have a georeferenced image and I'm using the code below to making the plot of points.
coordinates = data[,2:3]
groups = data[,4:9]
## Scale the groups so they are all between 0 and 1
max.groups = max(groups)
min.groups = min(groups)
scaled.groups = (groups-min.groups)/(max.groups-min.groups)
# Plot with color gradient
max.saz = max(groups$G1)
min.saz = min(groups$G1)
scaled.saz = (groups$G1-min.saz)/(max.saz-min.saz)
## Colors ranging from red (very) to blue (little)
saz.colors = rgb(red=scaled.saz,green=0,blue=(1-scaled.saz))
plotG1 = plot(coordinates$long, coordinates$lat, pch = 16, cex = 2, col = saz.colors, bg = saz.colors, xlab="Longitude (Wº)", ylab="Latitude (Sº)")
Upvotes: 0
Views: 1281
Reputation: 78792
Here's a way to do it with ggplot2 (riffing off of @RobertH):
library(raster)
library(rgeos)
library(ggplot2)
library(ggthemes)
library(viridis)
# retrieve map
bra <- getData('GADM', country='BRA', level=0)
# simplify the map so it plots more quickly
bra_simpl <- gSimplify(bra, 0.01, topologyPreserve=TRUE)
# make it work with ggplot2
bra_map <- fortify(bra_simpl)
# simulate some data
dots <- data.frame(x=c(-44.53, -38.61, -35.91, -37.96, -42, -35.1,
-40.5, -45.83, -37.11, -43.85),
y=c(-1.9, -12.74, -9.88, -12.5, -2.79, -8.69, -20.67,
-1.19, -4.94, -2.48),
value=sample(100, 10))
gg <- ggplot()
# lay down base layer
gg <- gg + geom_map(map=bra_map, data=bra_map,
aes(x=long, y=lat, map_id=id),
color="#2b2b2b", size=0.15, fill=NA)
# plot your points
gg <- gg + geom_point(data=dots, aes(x=x, y=y, color=value), size=4)
# better colors
gg <- gg + scale_color_viridis()
# projection & limit map viewport
gg <- gg + coord_map(xlim=c(-53,-34), ylim=c(-30,0))
# clean map plot
gg <- gg + theme_map()
# legend on right
gg <- gg + theme(legend.position="right")
gg
geom_raster
can work with fortified, georeferenced images.
Upvotes: 0
Reputation: 47146
Here is a general example:
library(raster)
bra <- getData('GADM', country='BRA', level=0)
crd <- matrix(c(-44.53, -38.61, -35.91, -37.96, -42, -35.1, -40.5, -45.83, -37.11, -43.85, -1.9, -12.74, -9.88, -12.5, -2.79, -8.69, -20.67, -1.19, -4.94, -2.48), ncol=2)
plot(bra)
points(crd, pch=20, col=topo.colors(10), cex=2)
To get just the coastline:
x <- as(bra, 'SpatialLines')
plot(x)
y <- crop(x, drawExtent())
# draw a box on the plot by clicking in two corners
# wait for 10 secs.
Now
plot(y)
points(crd, pch=20, col=topo.colors(10), cex=2)
But you say you have a georeferenced image. In principle (assuming that it has the same coordinate reference system) you should be able to do something like this:
r <- raster("image.tif")
plot(r)
points(crd, pch=20, col=topo.colors(10), cex=2)
Upvotes: 1