I am trying to plot Russia from a shp-file, but Russia always comes up as in two parts. How do I get Russia back together in one piece?
I have tried several shp-files e.g.
http://www.naturalearthdata.com/downloads/10m-cultural-vectors/
Admin 1 – States, Provinces ; Download without large lakes (14.11 MB) version 3.0.0
shp <- readOGR("ne_10m_admin_1_states_provinces_lakes.shp")
Subset to Russia
names(shp)
rus <- shp[shp$admin == "Russia" , ]
x11()
plot(rus)
Since you want to plot it, you can fortify the shapefile to data frame, modify the longitude coordinates in the -180 region, & plot the result in ggplot:
library(rgdal); library(ggplot2)
# read in the shapefile & subset to Russia
shp <- readOGR(dsn = "ne_10m", layer = "ne_10m_admin_1_states_provinces_lakes")
rus <- shp[shp$admin == "Russia", ]
# fortify to data frame & modify longitude coordinates in the -180 region.
rus.fortify <- fortify(rus) %>%
mutate(long = ifelse(long < -100, long + 180*2, long))
# plot to verify
ggplot(rus.fortify,
aes(x = long, y = lat, group = group)) +
geom_polygon(fill = "white", col = "black") +
coord_map() #default projection is mercator
I've also just learned from this post that you can specify different projection systems. Given how large Russia is, this ought to be relevant:
ggplot(rus.fortify,
aes(x = long, y = lat, group = group)) +
geom_polygon(fill = "white", col = "black") +
coord_map("azequalarea")
Related
I am working to create a map in ggplot2 using the geom_sf() function. The data for the map is from a KML file (downloaded from GoogleEarth), so when I read that into R, the lat/longs for each datapoint are in a combined 'geometry' column rather than individual lat/long columns. I need to add labels for each point onto my map, but since everything is in the 'geometry' column, I do not have the proper inputs for the aes() function, when it asks for 'x' and 'y' values.
My data is currently arranged like this:
Name Description Island BeachName Density geometry
1 C28 St. Croix Boiler Bay B POINT Z (-64.57273 17.75859 0)
2 C27 St. Croix Brown Bay A POINT Z (-64.57583 17.75935 0)
3 C39 St. Croix Buck Island Reef NM C POINT Z (-64.62571 17.78738 0)
4 C1 St. Croix Sandy Point NWR C POINT Z (-64.89964 17.67955 0)
5 C10 St. Croix Rainbow Beach A POINT Z (-64.88799 17.72994 0)
I have tried using geom_text():
stc_plot <- ggplot() +
geom_sf(data = stc_land, color = "grey40", fill = "grey80") +
geom_sf(data = stcdens, aes(size = Density)) +
geom_text(data = stcdens, aes(label = Name)) +
labs(size = "Nesting Activities per Year") +
scale_size_discrete(labels = c("<25", "25-100", "100-500")) +
theme_classic() +
theme(legend.position = "bottom")
And I get an error:
Error in `check_required_aesthetics()`:
! geom_text requires the following missing aesthetics: x and y
Is there a way to get the geometry column to be read out for labels? Or would I need to split that column so that the lat/longs are separate? The geometry column has worked fine for all other aspects of creating this map, its just the labeling that is giving me trouble...
You need to substitute your ggplot2::geom_text() for ggplot2::geom_sf_text() - the sf part is important.
For an example consider this plot of 3 semi random North Carolina cities (did I mention I <3 the nc.shp file?)
library(sf)
library(ggplot2)
shape <- st_read(system.file("shape/nc.shp", package="sf")) # included with sf package
cities <- data.frame(name = c("Raleigh", "Greensboro", "Wilmington"),
x = c(-78.633333, -79.819444, -77.912222),
y = c(35.766667, 36.08, 34.223333),
population = c("high", "medium","low")) %>%
st_as_sf(coords = c("x", "y"), crs = 4326)
ggplot() +
geom_sf(data = shape, fill = NA) +
geom_sf_text(data = cities, aes(label = name))
I have a shapefile here: https://login.filesanywhere.com/fs/v.aspx?v=8c6c63865a6574bcaa69
I have a shapefile of California red legged frog that I am overlaying on top of California, however, the range of these frogs extends outside of California and going into Mexico. I only want the frog data from California, how can I trim data extending into Mexico? I tried to use subset to separate the 'ORIGIN' but it doesn't seem to have any effect. Thanks for any help beforehand..
library(rgdal)
library(tidyverse)
ranas <- readOGR(dsn = ".", layer = "data_0")
names(ranas)
# Coerce into a dataframe
ranas4 <- fortify(ranas)
head(ranas4)
cali_map <- map_data("state",region="california")
counties <- map_data("county",region="California")
head(counties)
windows(w=9)
ggplot(cali_map, aes(x = long, y = lat, group = group)) +
geom_polygon() +
geom_polygon(data = ranas4, fill = "green")
ggplot(cali_map, aes(x = long, y = lat, group = group)) +
geom_polygon(fill = "cornsilk4", col = "cornsilk") +
geom_polygon(data=counties,color="black",fill="lightgoldenrod") +
geom_polygon(data = ranas4, fill = "green", alpha = 0.5) +
theme_void() +
theme(panel.background = element_rect(fill = "cornsilk")) +
coord_map("albers",lat=90,lat1=80)
# Tried to trim data outside California (Mexico data) with no success.
#I tried:
ranas2 <- subset(ranas,ORIGIN !=1)
but it doesn't have any effect or subsets anything.
Subsetting your spatial dataframe won't be of much use, because neither of its features (rows) is entirely within California:
## plot features by origin:
library(ggplot2)
library(sf)
my_shp <- read_sf(dsn = "path_to_shp_directory")
my_shp %>%
ggplot() +
geom_sf() +
facet_wrap(~ ORIGIN)
You can still crop (clip) a feature with California's boundaries ...:
## make sure both geometries have the same CRS,
## if necessary, by st_transform(your_epsg_code)
my_shp_cropped <- st_crop(my_shp, cali_map)
... but note that won't recalculate the underlying frog data (e.g. frog count in California only).
I am looking for a simple alternative to map_data('world') as a data set with which to easily draw the countries of the world. Ideally it's easily implementable with ggplot2.
The problem is that I am graphing some geographic points, some of which appear close to -90 (S). When I use map_data('world') to get my polygons, I see that Antarctica doesn't run all the way to -90 (lowest lat value for Antarctica is -85.19218 - see code below). Thus anything further south than that shows as if its off the map, which doesn't look great.
Here is an example of what I am talking about:
library('ggplot2') #Import library
world = map_data('world') #Get polygon data
data = data.frame(lat = -86, long = 0) #Create geographic data with southerly value
#plot
ggplot() +
geom_polygon(data = world,
aes(y = lat, x = long, group = group), col = 'grey', fill = NA) +
geom_point(data = data,
aes(y = lat, x = long), col = 'red')
Gives:
You can see the limits of the Antarctic extent as follows:
library('dplyr')
world %>%
filter(region == 'Antarctica') %>%
select(lat) %>%
min()
[1] -85.19218
Thus the problem is not the projection, its the data set. Does anyone know an alternative easily available rendering of the countries that has Antarctica running to -90?
There isn't any data for ggplot2 to plot below about 85 degrees south in the world data as a polygon. Using geom_polygon also has the drawback of connecting the data from the far left (west) to the data on the far right(east).
To get around having the straight line above the red dot, use the sf package and geom_sf to plot spatial data.
I don't know of a good way to plot Antarctica along with the entire rest of the globe. If you are mostly interested in the southern pole & it's surroundings, an orthographic projection might work.
library(tidyverse)
library(rnaturalearth)
world <- map_data('world') #Get polygon data
data = data.frame(lat = -86, long = 0) #Create geographic data with southerly value
#plot
p_polygon <- ggplot() +
geom_polygon(data = world,
aes(y = lat, x = long, group = group), col = 'grey', fill = NA) +
geom_point(data = data,
aes(y = lat, x = long), col = 'red')
# use rnaturalearth package to get coastline data in the sf format
world_sf <- ne_coastline(returnclass = 'sf')
# use geom_sf to plot the data
p_sf <- ggplot(world_sf) +
geom_sf() +
geom_point(data = data, aes(y = lat, x = long), col = 'red')
# geom_sf, using an orthographic projection
p_sf_ortho <- ggplot(world_sf) +
geom_sf() +
geom_point(data = data, aes(y = lat, x = long), col = 'red') +
coord_sf( crs= "+proj=ortho +lat_0=-80 +lon_0=90")
# the three plots together
cowplot::plot_grid(p_polygon, p_sf, p_sf_ortho,
labels = c('polygon', 'sf', 'sf orthographic'))
Created on 2021-08-27 by the reprex package (v0.3.0)
The {rnaturalearth} package has a good representation of Antarctica (and the other land masses too!) I have switched to using it for all my background mapping. This example:
world <- rnaturalearth::ne_countries(returnclass = "sf")
map_crs <- st_crs(3031) # WGS 84 / Antarctic Polar Stereographic
ggplot() + geom_sf(data = world %>% st_transform(map_crs)) +
coord_sf(datum = map_crs,
ylim = c(-3e6,3e6),
xlim = c(-3e6,3e6))
Produces this map - there is just a tiny sliver of unmapped Antarctica:
Hi I'm trying to plot the map of King County in Washington because I'm having few data points which need to be placed in the map. The following is what I used.
long <- c(47.5112,47.7210 ,47.3684,44)
lat <- c(-122.257, -122.319, -122.031,-120)
price <- c(287655,456355,662500,234563)
House <- data.frame(long, lat, price)
states <- map_data("state")
# west_coast <- states %>%
# filter(region %in% c("washington"))
wa_df <- states %>%
filter(region == "washington", subregion == 'king')
counties <- map_data("county")
wa_county <- counties %>%
filter(region == "washington", subregion == 'king')
wa_base <-
ggplot(data = wa_df,
mapping = aes(x = long, y = lat, group = group)) +
geom_point(
data = House,
mapping = aes(x = long, y = lat),
color = "red",
inherit.aes = FALSE
) +
coord_fixed(1.3) +
geom_polygon(color = "black", fill = "gray")
#geom_point(data = House, mapping = aes(x = long, y = lat), color = "red")
wa_base + theme_nothing() +
geom_polygon(data = wa_county, fill = NA, color = "black") +
geom_polygon(color = "black", fill = NA) # get the state border back on top
The following is the map I received. I do not find that the map looks good. Please help
I am new to R and this is my first response, but I might have found a solution to this problem. The boundaries downloaded from the maps package are not exact. You can download correct boundary data as a shapefile here: https://gis-kingcounty.opendata.arcgis.com/datasets/kingcounty::king-county-political-boundary-no-waterbodies-kingco-area/explore?location=47.477749%2C-121.920728%2C9.83
Use:
library(ggmap)
library(mapdata)
library(rgdal)
library(tidyverse)
After that, read the shapefile into R and use map_data() to convert it into a data frame:
kc_bound <- readOGR(dsn="~/Desktop/King_County",layer="King_County_Political_Boundary_(no_waterbodies)___kingco_area")
kc_bound_df <- map_data(kc_bound)
Assuming that you also work on the house price prediction data set from kaggle (https://www.kaggle.com/harlfoxem/housesalesprediction) you can run the following code to get a plot of the houses with the King County boundary around it (df is your data frame):
ggplot() + geom_point(df,mapping=aes(x=long,y=lat),color="red") + geom_polygon(kc_bound_df,mapping=aes(x=long,y=lat),fill=NA, color="black")
See this picture for the result: Houses in King County plotted with county boundary
Hope this helps, let me know what you think!
I'm trying to plot ecological distribution of some species of organisms I'm studying over the Arabian/Persian Gulf. Here is a sample of a code I've tried:
Backround layer
library(ggplot2)
library(ggmap)
nc <- get_map("Persian Gulf", zoom = 6, maptype = 'terrain', language = "English")
ncmap <- ggmap(nc, extent = "device")
Other layers
ncmap+
stat_density2d(data=sample.data3, aes(x=long, y=lat, fill=..level.., alpha=..level..),geom="polygon")+
geom_point(data=sample.data3, aes(x=long, y=lat))+
geom_point(aes(x =50.626444, y = 26.044472), color="red", size = 4)+
scale_fill_gradient(low = "green", high = "red") + scale_alpha(range = c(0.00, 0.25), guide = FALSE)
but, I will like to use the stat_density2d to show the distributions of hundreds of species (which are recorded in columns e.g SP1....SPn) over the water body rather than just displaying latitude and longitude.
Also, is it possible to restrict my heat map to just the water body?
I'll appreciate any help and recommendations I can get on this please
My approach to your question is a pragmatic one: simply put the layer of gulf countries over the heatmap distribution. This crops the heatmap accordingly. Note, however, that the heatmap is still calculated as if it weren't cropped. That means the density calculation is not restricted to the water body only, but it is simply cropped visually.
For the sake of reproducibility, the following code assumes that you've unzipped the .rar file provided by #Hammao and execute the code in the resulting Persian Gulf folder.
# get sample data
sample.data <- read.csv("sample.data3.csv")
Now, we need to get the country shapes for the Gulf countries. I use the rworldmap package for this.
# loading country shapes
library(rworldmap)
# download map of the world
worldmap <- getMap(resolution = "high") # note that for 'resolution="high"'
# you also need the "rworldxtra" pkg
# extract Persian Gulf countries...
gulf_simpl <- worldmap[worldmap$SOVEREIGNT == "Oman" |
worldmap$SOVEREIGNT == "Qatar" |
worldmap$SOVEREIGNT == "United Arab Emirates" |
worldmap$SOVEREIGNT == "Bahrain" |
worldmap$SOVEREIGNT == "Saudi Arabia" |
worldmap$SOVEREIGNT == "Kuwait" |
worldmap$SOVEREIGNT == "Iraq" |
worldmap$SOVEREIGNT == "Iran", ]
# ... and fortify the data for plotting in ggplot2
gulf_simpl_fort <- fortify(gulf_simpl)
# Now read data for the Persian Gulf, which we need to get the distances for
# the extension of the map
PG <- readOGR(dsn = ".", "iho")
PG <- readShapePoly("iho.shp")
PG <- fortify(PG)
Now, it's simply a matter of plotting the layers in the correct order.
# generate plot
ggplot(sample.data) +
# first we plot the density...
stat_density_2d(aes(x = long, y = lat,
fill = ..level..),
geom="polygon",
alpha = 0.5) +
# ... then we plot the points
geom_point(aes(x = long, y = lat)) +
# gradient options
scale_fill_gradient(low = "green", high = "red") +
scale_alpha(range = c(0.00, 0.25), guide = FALSE) +
# and now put the shapes of the gulf states on top
geom_polygon(data = gulf_simpl_fort,
aes(x = long,
y = lat, group = group),
color = "black", fill = "white",
inherit.aes = F) +
# now, limit the displayed map only to the gulf
coord_equal(xlim = c(min(PG_fort$long), max(PG_fort$long)),
ylim = c(min(PG_fort$lat), max(PG_fort$lat))) +
theme_bw()