I'm trying to plot some coordinates that stretches across Antarctica, and since I really don't care about the rest of the world, I'm trying to find a way to plot just the Antarctic continent with R (i.e. showing a view from the south pole). Problem is, nobody seems to care about Antarctica and most of the stuff I'm able to find about how to plot coordinates in R straight up cut away the continent because it gets warped in the Mercator projection...
and now I'm completely stuck. Any advices on how to approach the problem? Or helpful sources?
Have you tried using a UTM projection in conjunction with leaflet?
You need to transform to a polar stereographic projection system.
require(maptools)
shp = wrld_simpl[which(wrld_simpl$NAME=="Antarctica"),]
shp = spTransform(shp,"+init=EPSG:3031")
plot(shp)
Related
I am trying to overlay a shapefile representing the range of Coast Redwoods onto some other data I'm processing in geoviews. I can successfully plot the data using cartopy and matplotlib. GeoPandas reads the shapefile, but passing the GeoDataFrame or individual shapely polygons to gv.Shape (as in the geoviews user guide under "Shape") consistently results in
AttributeError: 'list' object has no attribute 'xy'
I am not sure whether the problem is in Shapely or in Geoviews. I suspect geoviews because geopandas is able to reproject and plot the polygons.
I've put up a notebook demonstrating the problem and providing the shapefile.
Any help or ideas much appreciated.
I was having the same issue, then i went ahead and converted my GeoDataFrame to EPSG:4326 as follows:
projected_df = original_df.to_crs('EPSG:4326')
Originally I was in EPSG:4269 (Albers Equal Area) and thought adding that to an Albers Equal Area projection would work but I think you need to start your data with EPSG:4326 and then work with projections with Geoviews.
Let me know if that does the trick.
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
I'm using R 3.1.2, trying to create a choropleth using a world map. The quirk is that, because my audience is in Asia I need to recenter the map. From the documentation it seems like nowrapRecenter() is perfect for this, but I find that it doesn't seem to work as advertised. For example, start without any recentering:
library(maps)
library(maptools)
library(rgdal)
data(wrld_simpl)
plot(wrld_simpl)
Now try to recenter at 148E longitude, in order to move Asia closer to the centre of the map while splitting as few land masses as possible at the left/right margins:
library(maps)
library(maptools)
library(rgdal)
data(wrld_simpl)
world <- nowrapRecenter(wrld_simpl,offset=148,avoidGEOS=TRUE)
plot(world)
What you get is a bit messy. Not only is the map centred at 180E longitude, but there are scratches all across the map where polygons which nowrapRecenter() should have been divided and re-closed at the left/right are extending across the full width of the map. In fact recentering does not appear to work cleanly for any chosen offset.
A similar question came up before, and the final comment there provided an example using nowrapRecenter(), but it no longer seems to work. What's the best way to recenter a world map (using SpatialPolygons) and have the polygons at the left/right margins be properly divided?
Thanks!
This is only a partial answer, so if it's not adequate let me know and I'll delete it.
The problem appears to be that transforming a Mercator projection fails near the poles. If you are willing to exclude Greenland and Antarctica, this works.
library(maptools)
data(wrld_simpl)
wrld <- wrld_simpl[!(wrld_simpl$NAME %in% c("Greenland","Antarctica")),]
library(ggplot2)
ggplot(wrld,aes(x=long,y=lat,group=group))+
geom_polygon(fill="white",color="grey30")+
coord_map(orientation=c(90,0,148))+
scale_x_continuous(breaks=c(0,60,120,180,-120,-60))+
theme_bw()
Even with this restriction, nowrapRecenter(...) failed.
I have the following SpatialPolygonsDataFrame.
require(raster)
usa <- getData('GADM', country='USA', level=2)
metro <- subset(usa, NAME_1=="Nebraska" & NAME_2 %in% c("Dodge","Douglas","Sarpy","Washington"))
plot(metro)
I would like to be able to replicate the following map boundaries (defined by the colors):
Does anyone know a good plan of attack? I realize this is a somewhat manual process. I have already downloaded all US Census files that are of a more detailed geography. I was hoping that a more detailed level of geography could be aggregated to answer the above question, but unfortunately the districts do not line up the same.
Is there a R function already out there that would be helpful in assisting this manual process? At the very minimum, I would like to be able to leverage the perimeter of the 4-county area.
Use writeOGR from the rgdal package to create a shapefile of your metro object. Then install QGIS (http://www.qgis.org/), a free and open-source GIS, and load the shapefile as a new layer.
Then you can edit the layer, add new polygons, edit lines etc, then save as a shapefile to read back into R.
Additionally, you may be able to "georeference" your image (by identifying known lat-long points on the image) and load that into QGIS as a raster layer. That makes it easier to digitise your new areas. All you need for that is a few lat-long coordinates of specific points, such as the corners of polygons or line intersections, and then QGIS has a georeferencing plugin that can do it.
I don't think you'll find any R code as suitable for digitising new geometries over an image as good as QGIS.
After half an hour (and twenty years experience, not all of which you'll need) I've got this:
I didn't precisely digitise your new boundaries though, just roughly for speed. That QGIS screen cap shows the five coloured areas under the four metro areas.
Step one was georeferencing. This screengrab shows how the PNG has been georeferenced - the red line is the metro area shapefile drawn with transparency over the PNG after the PNG has been converted to a GeoTIFF by matching control points.
Step two was then using QGIS editing tools to split, join, and create new polygons. Then I just coloured them and added labelling to pretty it up.
I could probably bundle these files all up for you to neaten, but it really doesn't take that long and you'll learn a lot from doing it. Also, this is probably a gis.stackexchange.com question...
I'm brand new to any programming and I'm starting right in the deep end with R!
So, I'm trying to plot a map of some dive sites from which me and a buddy collected some data, which are off the west coast of the Isla de Juventud. I have plotted the map of Cuba and a more zoomed in one of the Island in R, and what I'm hoping to achieve is something along the lines of the following, but I just chucked this together with paint so obviously it would be better quality produced in R.
https://www.dropbox.com/sh/xve973hvih1vw77/OZ-gGpDgsJ <- this link
The code I have for the cuba map and the island map is this, respectively.
map("worldHires", xlim=c(-85.3198254, -73.718262), ylim=c(190663280, 23.5), fill=TRUE, col="darkseagreen3", bgcol="grey80") map.axes()
and
map("worldHires", xlim=c(-83.25, -82.5), ylim=c(21.4, 22), fill=TRUE, col="darkseagreen3", bg="gray80") rect(-83.2, 21.5, -83.15, 21.65, border="red")
I'd rather learn how to do it or where I'm going wrong, if anywhere, here as I really want to improve my skills with this program.
I think you are seeing this the wrong way.
I read the manual for R package Maps in (http://cran.r-project.org/web/packages/maps/maps.pdf), and I couldn't find a map database called "worldHires"
To have a better understanding, try the commands
map('world2')
map.axes()
This should give a plot of the world map; however, to highlight certain cities, read about map.cities command in the package manual.
To get a map of Cuba for example try this
map("world2", region='Cuba',fill=TRUE, col="darkseagreen3")
map.axes()
I hope this helps
Have you loaded maps, mapdata, and maptools? That fixed the same problem for me!