shade between two latitudes on country map - r

I want to plot a simple outline of a country map and have an area between two latitudes shaded grey.
I think I need to use the rworldmap library but I'm not exactly sure how to achieve what I want.

Good suggestions by others. I initially thought that you might want the latitudinal shading to extend beyond the country borders. If that's the case here's a quick and dirty solution using rworldmap for the map, sp to plot it and polygon from base graphics to add the latitude shading. You could then modify using spTransform from rgdal if you want to project, or the suggestion from Sam if you want to crop to country borders.
library(rworldmap)
library(sp)
sPDFworld <- getMap(resolution="low")
#getMap() maps have ADMIN & NAME fields with country names formatted differently
#use sPDFworld$ADMIN sPDFworld$NAME to check
country <- "United Kingdom"
#to plot just this country (uses sp)
plot(sPDFworld[ sPDFworld$ADMIN==country, ] )
#shade a latitude region
polygon(x=c(-180,-180,180,180),y=c(55,60,60,55),col=adjustcolor('grey',alpha.f=0.5),border=NA)

I'd do something as in https://stackoverflow.com/a/13986029/1358308 . You can crop down to your "rectangle" as needed and then just plot the interiour of the resulting polygon as "grey", and don't print the borders by setting them to NA.
If you plot this "after" the larger outline (using the same polygon, before doing the intersection) then you should just get the country filled in. If you want the "water" filled in using a different color, you can do this "first". The later plot(x, add=TRUE) will plot the new things "on top" so you won't see the working behind.
Hope that makes sense!

Related

Choropleth maps (plotly) with detailed borders

I am trying to plot a world map, where the colors correspond to a specific variable. However, it does not show small states like "Andorra" or "Liechtenstein", even though they are in the data frame (see df$COUNTRY). I think the problem is, that the borders are not drawn with much detail. Hence one can not see the small states. Is there a way draw the borders with more detail like with the packs, maps, and mapdata (e.g wolrd_detailed <- map_data ("worldHires")? Or any other way to make the small states visible? Here is the example from the plotly website. It does not show small states:
library(plotly)
df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv")
fig <- plot_ly(df, type='choropleth', locations=df$CODE, z=df$GDP..BILLIONS., text=df$COUNTRY, colorscale="Blues")
fig
Thanks for the help!

R: cropping/zooming a map

I am trying to overlay data on top of a map of Canada, however I can't adjust the zoom as I would like. In the map, I want to be able to see the lines for each province anong with its name (so using map("world", "Canada") isn't desirable)
I have tried altering the zoom, but one is too zoomed out and the other is too zoomed in:
qmap(location = "Canada", zoom = 3)
qmap(location = "Canada", zoom = 4)
I have tried researching on how to crop the image but have been unsuccessful
Thank you!
You can approach this using either the maps and mapsdata packages or continue with ggmap. It just depends on how much detail you want.
library(raster)
library(maps)
library(mapdata)
canada<-getData("GADM",country="CAN",level=1)
plot(canada,xlim=c(-141,-53),ylim=c(40,85),col="gray80",border="gray40",axes=T,las=1)
invisible(text(getSpPPolygonsLabptSlots(canada),labels=as.character(substr(canada$HASC_1,4,5)),
cex=0.75, col="black",font=2))
Using ggmap, you can specify a bounding box when grabbing your spatial data. It still overplots some of the area you are interested in (i.e., plots most of the US). Therefore, reapplying the bounding box values to the ggmap function cuts down the viewable area.
library(ggmap)
lats<-c(40,85)
lons<-c(-141,-53)
bb<-make_bbox(lon=lons,lat=lats,f=0.05)
cda<-get_map(bb,zoom=3,maptype="terrain")
ggmap(cda)+xlim(lons)+ylim(lats)+theme_bw()+labs(x="Longitude",y="Latitude")

Display only a single state with ggmap

Is there a simple way to display only a single state? For example
qmap("Texas", zoom=6)
produces
which obviously also includes all of Oklahoma, most of New Mexico, etc...is there a way to "mask" the surrounding states (and Mexico) to display only Texas?
The short answer is that you need to tell ggmap where Texas is. Typically you'd do that using a shapefile. See e.g.
R: ggmap – Overlay shapefile with filled polygon of regions
Plotting Choropleths from Shapefiles in R with ggmap

how to plot a part of map but with limitation arround it

how to plot something like this:
see image here, I didn't have 10 reputation so I can not post image, so paste a image URL here.
http://www.flickr.com/photos/97751721#N07/9089566951
or
http://www.flickr.com/photos/97751721#N07/9091786734
right part of this map is a zoom in map with limitation, that is what I want
if it is use R code or other program language will be more great!
If your have everything as Spatial object you could simply plot with standard plot() and set lim to what ever you like.
require(maptools)
shape <- readShapePoints("shapefile.shp")
plot(shape, xlim=c(minXcoordinate, maxXcoordinate) ylim=c(minYcoordinate, maxYcoordinate))
It is a little unclear to me what exactly you want to do. Do you just want to make a map with a specific set of lon/lat boundaries? Do you need to plot data on top of it? Do you need to control the appearance to make it look like the example you give (with line boundaries & minimal geographic information)?
The ggmap package may get you started on this. The syntax goes like this:
require(ggmap)
# The location argument defines the center of the map
exampleMap <- get_map(location = c(lon = -95, lat = 30), zoom=5)
ggmap(exampleMap)
You can then add data to the map if you like (exactly how is left as an exercise to the motivated student!)

Overlaying R county map onto a state map

R newbie here.
If I first use map('state'), how can I then use map('county', ylim=..., xlim=...) but offset it like this:
Right now, I am using imagemagick's composite -gravity southwest ... to combine 2 PNG files, but this seems less than ideal.
You can use the fig option in a graphics parameter call to set the region where a figure will be plotted.
map("state")
par(fig=c(0.1,0.9,0,0.6), new=TRUE, xpd=NA)
map("county",regions="california",add=TRUE)
In regards to the other code, xpd=NA lets you plot in the margins of a plot while new=TRUE makes sure you don't scrap the existing plot when starting a new plot. Adding add=TRUE to the end of the plot for the county acts similarly to the new=TRUE call.

Resources