A few lines of code to expose my problem. When I work with a map of
the world and I introduce a projection, I always end up with some
weird looking horizontal lines.
Please have a look at
https://www.rdocumentation.org/packages/ggplot2/versions/1.0.0/topics/coord_map
from where I take the example for New Zeland
library(ggplot2)
nz <- map_data("nz")
# Prepare a map of NZ
nzmap <- ggplot(nz, aes(x = long, y = lat, group = group)) +
geom_polygon(fill = "white", colour = "black")
# Plot it in cartesian coordinates
nzmap
# With correct mercator projection
nzmap + coord_map()
which works beautifully. Now let us do the same with the world
world <- map_data("world")
# Prepare a map of the world
worldmap <- ggplot(world, aes(x = long, y = lat, group = group)) +
geom_polygon(fill = "white", colour = "black")
# Plot it in cartesian coordinates
worldmap
##but the following is a disaster!
# With correct mercator projection
worldmap + coord_map()
I see this issue of the horizontal lines with a projection has been
going on for quite a while, but I was able to find only seasoned posts
and I had assumed this was fixed long ago.
Please find below my sessionInfo.
Is there any solution to this? Is it still an open bug?
This is a pretty common problem in ggplot, but happily it is easily fixed:
worldmap + coord_map(xlim=c(-180,180))
produces
solution from: Why does coord_map produce a weird output?
Related
I'm trying to display labels on GIS polygon features in R using the st_centroid function in the sf library. Unfortunately, while the head() function seems to show that each polygon has different x and y coordinates associated with it, all labels get rendered overlapping at a single point on the map (which is apparently the centroid of one particular polygon). What am I doing wrong here?
Current code setup:
library("ggplot2")
library("sf")
sf::sf_use_s2(FALSE) #makes centroids not break
world <- st_read("C:/prgrm/gis/source/10m_land_and_islands.shp")
prov <- st_read("C:/prgrm/gis/edited ncm/ncm_provinces.shp")
prov <- cbind(prov, st_coordinates(st_centroid(prov))) #attaches centroids to 'prov' dataset
head(prov)
ggplot(data = world) +
geom_sf() +
geom_sf(data=prov, aes(fill="blue")) +
geom_text(data=prov, aes(X,Y, label=provname_r), size=5) +
coord_sf(xlim=c(-2000000,1000000),ylim=c(-1500000, 3000000), crs=st_crs(3310))
You may be better off with specifying the centroid placement via fun.geometry argument of the geom_sf_text() call / by the way the default is sf::st_point_on_surface() - which is a good default as it makes sure that the label is not placed inside a hole, should the polygon have one.
Consider this example, using the well known & much loved nc.shp shapefile that ships with {sf}.
library(sf)
library(ggplot2)
# in place of your world dataset
shape <- st_read(system.file("shape/nc.shp", package="sf")) # included with sf package
# in place of your prov dataset
ashe <- shape[1, ]
ggplot(data = shape) +
geom_sf() +
geom_sf(data = ashe, fill = "blue") +
geom_sf_text(data = ashe,
aes(label = NAME),
color = "red",
fun.geometry = st_centroid)
How can I plot GPS trajectory over road and zoom on that road?
Can someone please take a point (40.74640013950355, -73.98755303328286, in Manhattan) and plot it over the corresponding road network [may be a grid 600ft by 600ft]. Please edit the code below to illustrate -
lat <- 40.74640013950355
long <- -73.98755303328286
tbl <- tibble(lat, long)
ggplot(data = tbl,
aes(x = lat,
y = long)) +
geom_point()
Once I know how to plot the road and I can overlay my trajectory data by modifying tbl above.
Thanks
There is no big difficulty to achieve such plot, starting from the example given in tigris library:
library(tigris)
library(ggplot2)
library(ggthemes)
roads <- roads("Maine", "031")
gg <- ggplot() + geom_sf(data = roads, color="black", fill="white", size=0.25) + theme_map()
lat <- 43.5; long <- -70.6; bbox = 0.02
bbox_gg = coord_sf(xlim=c(long-bbox/2, long+bbox/2), ylim=c(lat-bbox/2, lat+bbox/2))
gg + geom_point(data=data.frame(lat, long), aes(x=long, y=lat), size=4, color="red") + bbox_gg
What is done here is just adding a geom_point() aesthetic on top of the geom_sf() layer. We can use a kind of bounding box coordinate limit to adjust the zoom
EDIT
If you need some road names on your map, you can add this to the plot:
geom_sf_label(data=subset(roads, roads$RTTYP!="M"), aes(label=FULLNAME))
here I use subset to avoid plotting all little road names. Eventually, you might want to find a way to zoom/subset your data before plotting, because it's gonna be too long to do it like this.
I've done a lot of searching on this topic and have yet to find anything that seems to solve my problem. I am working on plotting a road segment and overlaying some radar data that I have. Below is an example of what I am working on.
I've generated the above plot with the following ...
map <- get_map(
location = c(-86.34, 39.94),
source = "google", zoom = 13, maptype = "roadmap"
)
lon <- c(-86.355750256169358, -86.357100144855735, -86.359359890246708)
lat <- c(39.949089789262416, 39.950209921850444, 39.952050262109935)
roadDf <- data.frame(lon, lat)
ggmap(map) + geom_tile(data = mrms_sub, aes(x = longitude, y = latitude, fill = palmer)) +
geom_point(data = mrms_sub, aes(x = longitude, y = latitude)) +
geom_point(data = roadDf, col = "red")
I currently have my road segment showing as the three points from my WKT LINESTRING as a means of visualizing it but ultimately I want to show it as a line segment on the map. I've tried using rgeos readWKT function which makes a "Formal class SpatialLine". If I plot it using,
plot(roadSegment)
it works but I can't make it work with my ggplot. I've tried using geom_lines and geom_segments but it throws the following errors.
geom_segment(roadSegment, aes(x=longitude, y=latitude))
#Error: ggplot2 doesn't know how to deal with data of class uneval
geom_line(line, aes(x=longitude, y=latitude))
#Error: Mapping must be created by `aes()` or `aes_()`
My linestring is the following is read in like so ...
#define roads
road1 <-("LINESTRING(-86.355750256169358 39.949089789262416, -86.357100144855735 39.950209921850444, -86.359359890246708 39.952050262109935)")
roadSegment <- rgeos::readWKT(road1)
Any help would be greatly appreciated!
I am looking for a means of plotting raster data (using ggplot and geom_raster) on a small scale map. I would like to use ggalt and coord_proj to 'zoom-in' on particular regions of the shapefile, but I run into the error geom_raster only works with Cartesian coordinates
ggplot() +
geom_polygon(data = land_df, aes(long, lat, group = group), fill = 'grey25')+
geom_raster(data = df, aes(lon_bin, lat_bin, fill = sum_hours)) +
coord_proj(xlim = c(-67, -63),ylim = c(0, 9))
Is there another simple means of generating zoomed in raster maps using coord_proj and avoiding this restriction that geom_raster only works with Cartesian coordinates?
The other options I can think of is to generate individual shapefiles for each 'zoomed-in' region, but I am plotting many of these rasters and would rather not have to generate individual shapefiles for each and instead use coord_proj to programmatically specify raster map limits.
Thanks
I think you need to use geom_tile() instead of geom_raster(). geom_raster() internally uses a rasterGrob, which is a bitmap that can only be scaled linearly. Hence the limitation to Cartesian coordinate systems. geom_tile() draws individual rectangles which can be transformed into any coordinate system.
I don't have your dataset, but I can show a very simple example:
df <- data.frame(x = 1:100) # a very simple dataset
p_raster <- ggplot(df, aes(x, fill = x, y = 0)) +
geom_raster() +
scale_fill_distiller()
p_raster
p_raster + coord_polar()
## Error: geom_raster only works with Cartesian coordinates
Now with geom_tile():
# for geom_tile(), map both fill and color to avoid drawing artifacts
p_tile <- ggplot(df, aes(x, color = x, fill = x, y = 0)) +
geom_tile() +
scale_fill_distiller() +
scale_color_distiller()
p_tile
p_tile + coord_polar()
I am attempting to plot several shapefiles on top of a map generated through ggmap. This is working well, however I want to constrain the view area to the shapefile (and not rely on the zoom argument in ggmaps). I've done this by getting the bounding box and passing it as an argument in ggplot's coord_cartesian While this works, I am getting some tearing issues on the edges of the map - most specifically on the western portion. I've tried adjusting the x-y coordinates manually but it seems to only severely distort the picture.
My thoughts are to zoom out slightly to allow the entire shapefile to be plotted in the area, but I can't seem to figure it out. It's also possible I am going about this entirely in the wrong way.
Here's the code I used to generate the map. The shapefile can be downloaded here
library(dplyr)
library(ggmap)
library(rgdal)
library(broom)
# Read in shapefile, project into long-lat
# Create 'tbox' which is a minimum bounding box around the shapefile
tracts <- readOGR(dsn = ".", layer = "CensusTracts2010") %>%
spTransform("+proj=longlat +ellps=WGS84")
tbox <- bbox(tracts)
# Plot data
tract_plot <- tidy(tracts)
DetroitMap <- qmap("Detroit", zoom = 11)
DetroitMap + geom_polygon(data = tract_plot, aes(x = long, y = lat, group = id), color = "black", fill = NA) +
coord_cartesian(xlim = c(tbox[1,1], tbox[1,2]),
ylim = c(tbox[2,1], tbox[2,2]))
I followed your workflow, which resulted in the same problem as you mentioned above. Then I changed the zoom on the qmap option from 11 to 10 and it resulted in a much better picture, although you do lose some of the place names but you can add those in manually yourself with annotate:
DetroitMap <- qmap("Detroit", zoom = 10)
DetroitMap + geom_polygon(data = tract_plot, aes(x = long, y = lat, group = id), color = "black", fill = NA) +
coord_cartesian(xlim = c(tbox[1,1], tbox[1,2]),
ylim = c(tbox[2,1], tbox[2,2]))