Plotting points on a map - r

I am trying to plot coordinate points on a map, but I get the plot.new error. Could you please help?
library(maptools)
library(ggmap)
library(mapproj)
table <- read.table("table.txt", header=TRUE, sep=",")
map <- get_map(location = 'France', zoom = 6, maptype = c("toner"))
points(table$LONG, table$LAT, pch=21, bg=color, cex=0.7, lwd=.4)
ggmap(map)
Here is an idea of what the table looks like:
CITY,LAT,LONG
Paris,48.856667,2.351944
Lyon,45.766944,4.834167
Bordeaux,44.838611,0.578334

Try geom_point:
library(maptools)
library(ggmap)
library(mapproj)
city <- c("Paris", "Lyon", "Bordeaux")
my.lat <- c(48.856667, 45.766944, 44.838611)
my.long <- c(2.351944, 4.834167, 0.578334)
points <- data.frame(lon=my.long, lat=my.lat)
map <- get_map(location = c(left = -5, bottom = 42, right=9, top = 51 ), source = 'stamen', maptype = 'toner')
france <- ggmap(map, extent = 'normal')
france + geom_point(data=points, col="red")
Try the command ?ggmap for a list of great examples. I think the manual has done a good job, because before I read your question, I didn't even know of any of these functions. Thanks! I've learned something new.

Learn to walk before you try and run.
The points function adds points to an existing graphic. You haven't got an existing graphic yet (unless you've already done something you've not showed us).
Hence if you do points before starting a plot, you'll get an error. eg:
points(1:10,1:10) # plot.new error
plot(1:10,1:10) # no error, starts a new plot
points(10:1,1:10) # adds extra points, no error.
All your stuff with ggplot is irrelevant. Also, this is not about statistics, so you should have posted to StackOverflow. I've flagged this and it might get migrated...

Related

Is it possible to use `locator()` with plots generated by sf?

I was wondering if it's possible to use the locator() function to create new points with plots generated by plot.sf. I'm sorry but I don't know how to create a reproducible example, so I will do my best to explain my problem.
When I run the following code (clicking, for example, on the centroid of some non-blue regions), the locator() returns the coordinates of the selected points. They have a reasonable value. However, when I plot them, the results are not that accurate (i.e. none of the chosen point lie in the selected region). Can you explain to me what's the problem?
library(sf)
nc = st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE)
plot(nc["SID74"], reset = FALSE)
pts <- locator()
pts <- st_sfc(st_multipoint(do.call(cbind, pts)), crs = st_crs(nc))
plot(pts, add = TRUE, pch = 16)
This is an interesting problem; I don't have an answer but I suspect that the issue will be environment related.
I ran your code on Linux (with a small adjustment for target number of points / clicks) and the code behaves as expected. I was able to place three points inside the Mecklenburg (as in Charlotte of Mecklenburg-Strelitz) county.
As locator() seems to use X11 (which is in my case native, but on Windows wrapped in a kind of convoluted fashion) I speculate that your problem may be environment related.
library(sf)
nc = st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE)
plot(nc["SID74"], reset = FALSE)
pts <- locator(n = 3) # to speed up the process
pts <- st_sfc(st_multipoint(do.call(cbind, pts)), crs = st_crs(nc))
plot(pts, add = TRUE, pch = 16)

Add points (Lat-Lon) to a marmap plot

I would like to plot a series of lat-lon points of a seal track, each coloured according to an attribute, on to a map that shows the bathymetry (100m contours) and coastline. I learnt how to create a map to show the bathymetry+coastline using marmap and ggplot2. The code is here:
dat <- getNOAA.bathy(-58,-62.5,43,46.0,res=0, keep=TRUE)
plot(dat,image=TRUE,bpal = list(c(min(dat), 0, "darkblue", "blue","lightblue"), c(0, max(dat), "gray90","gray10")),drawlabels=TRUE,deep=c(-500,200,0),shallow=c(-500,100,0),step=c(500,100,0),lwd=c(1,1,1),lty=c(1,1,1),land=TRUE)+
scaleBathy(dat, deg=1.232, x="bottomleft", inset=5) #100km
This created a useful map. However, I am stalled over how to add the seal track on to this map.
I could do this in ggmap (using the code below) but I much prefer the marmap map
myLocation <- c(-62.5,43,-58,46)
seal_map2<-get_map(location=myLocation,maptype="watercolor",source="stamen",zoom=10)
ggmap(seal_map2)+
geom_point(data=sealtrack,aes(color=category),size=0.5)+
scale_color_gradientn(colours=rainbow(6), breaks=seq(1,6,by=1))
Any guidance will be much appreciated
You should be able to add the bathymetric info from marmap as a contour layer on your plot after "fortifying" it. Without your data it's difficult to make sure that it works (and the NOAA server is down for me right now):
library(ggplot2)
library(marmap)
dat <- getNOAA.bathy(-58,-62.5,43,46.0,res=0, keep=TRUE)
dat <- fortify(dat)
ggmap(seal_map2) +
geom_contour(dat, aes(x = x, y = y, z = z)) +
geom_point(data=sealtrack,aes(color=category),size=0.5) +
scale_color_gradientn(colours=rainbow(6), breaks=seq(1,6,by=1))

Map in R, how do I draw a map of Baltimore?

I'm trying to do a Shiny app, and a part of it, consists into a plot of Baltimore city, and once I have this, I will print other data on this map.
I've been trying 'rworldmap', 'raster' and 'ggmap', but I'm not getting how to put the right parameters to make it work, the time i've been closer is this:
library(rworldmap)
newmap <- getMap(resolution = "low")
plot(newmap)
library(rworldmap)
newmap <- getMap(resolution = "low")
plot(newmap)
plot(newmap,
xlim = c(-39.3750, -76.7226),
ylim = c(39.1800, -76.5254),
asp = 1
)
It appears to me the south-america region, and... upside down :S
Latitude and longitude in theory should be aprox:
(39.371846,-76.720619)
(39.198588,-76.491280)
Anyone know how should I continue with this?
Sorry for my english,
Thanks! :)
P.S:If this can help, what I'll print later will be coordinates.
Using ggmap:
library(ggmap)
map <- get_map(location = 'Baltimore', zoom = 12)
ggmap(map)

Drawing line on ggmap plot between two countries using long/lat

I am a total newbie to R and I would like to draw a line (possibly weighted, e.g., by the number of trips made) between two countries. Currently, I use longitude and latitude for each capital to draw a line, but I would like to do it using the package ggmap. I was looking around, but did not find any solution so far. I would appreciate a quick help.
require(ggmap)
require (rworldmap)
all_content = readLines("ext_lt_intratrd_1_Data.csv")
skip_second = all_content[-2]
dat = read.csv(textConnection(skip_second), header = TRUE, stringsAsFactors =F)
dat[5,2]<- c("Germany") # using a data where the first line is
header, but second line must be skipped as it is EU 27
and not a single country
europe <- read.csv("eulonglat.csv", header = TRUE) # using world capitals to
generate points
myfulldata <- merge(dat, europe)
map <- get_map(location = 'Europe', zoom = 4)
mapPoints <- ggmap(map) + geom_point(aes(x = UNc_longitude, y = UNc_latitude, size
= log(myfulldata$Value)), data = myfulldata, col = "red", alpha= 0.5) # this can
be plotted
# i would continue with drawing line and i searched for references
# i found arrows(42.66,23.34,50.82,4.47) - which did not work
# i tried to look for a reference work more, but could not find
# instead i found it using with the package rworldmap the following
lines(c(4.47, 23.32), c(50.82, 42.66))
# this does not work on ggmap

Add units to scale bar in spplot()

I am working with R version 2.14.1-1 on Ubuntu 12.04, using spplot to plot GIS data, specifically 2008 vote tallies by precinct.
I am a bit new to R. I have researched my problem without finding a solution. I would like to add units to the scale bar, in this case something like "% of vote" running vertically down the bar.
My code so far is:
library(gpclib)
gpclibPermit()
library(maptools)
library(maps)
library(classInt)
library(RColorBrewer)
library(sp)
require(graphics)
library(rgdal)
library(lattice)
pennStateMap <- readShapeSpatial("/home/jgoodson/stateData/Pennsylvania/shapeFiles/pa_final.shp")
plotvar <- pennStateMap#data$USPP2008
nclr <- 10
class <- classIntervals(plotvar, nclr, style="equal")
poliPallet <- colorRampPalette(c("red1", "blue1"), space="Lab")
pennCounties <- unionSpatialPolygons(pennStateMap, as.numeric(pennStateMap#data$COUNTYFP10))
spCounties <- list("sp.polygons", as(pennCounties, "SpatialPolygonsDataFrame"), col.regions="transparent")
spplot(pennStateMap, "USPP2008", main="2008 Presidential Election Results by Precinct in Pennsylvania", col.regions=poliPallet(10), col="transparent", at=round(class$brks, digits=1), sp.layout = list(spCounties))
I tried adding the units via:
barLabel <- list("sp.text", c(40, -71), "Dem % of Two-Party Vote", srt=-90)
and then adding that to sp.layout. But this will not go outside the drawing box for the geographic data, so it disappears when I try to get to the correct side of the scale bar.
I would appreciate any help I can get on how to accomplish this.

Resources