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!
Related
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:
[US state with geom_path][1][US state with geom_point][2]I am using R to overlay US states shape file above ogallala region shape file. I would ideally like to have shape boundaries as line but I get poorly formed map when I try that (in pictures) but when I try geom_point it works alright. Can someone please explain what I am doing wrong. [US state with geom_line][3]
OG_HUC = read.csv("input/Ogallala_huc.csv")
OG_table =right_join(HUC8_map.df,OG_HUC,by = c("HUC_CODE"="HUC8"))
#OG_table = merge(HUC8_map.df,OG_HUC,by = "HUC8", sort = FALSE)
OG_table[is.na(OG_table)] = 0
#write.csv(OG_table,'OG_table.csv')
State <- readOGR(
dsn= paste0(getwd(),"/input/State") ,
layer="states"
)
State_map <- spTransform(State, CRS("+proj=longlat +datum=WGS84"))
State_map#data$id = rownames(State_map#data)
State_map.points = fortify(State_map, region="id")
centroids.df <- as.data.frame(coordinates(State_map))
names(centroids.df) <- c("Longitude", "Latitude") #more sensible column names
State_map.df1 = merge(State_map.points, State_map#data, by="id")
State_map.df2 = data.frame(id = State_map#data$id, State_map#data, centroids.df)
ggplot()+geom_polygon(data=OG_table,aes(x = long, y = lat, group=group),fill="lightskyblue",col="black", alpha = 0.3) +
geom_text(data = State_map.df2, aes(Longitude, Latitude, label=STATE_ABBR),col="black")+
#geom_path(data = OG_table, aes(long, lat, group=group),color="black") +
geom_point(data = State_map.df1, aes(long, lat, label=STATE_ABBR),color="black")+
coord_map(xlim = c(-108,-95),ylim = c(31,45))+
scale_fill_identity()
```enter image description here
[1]: https://i.stack.imgur.com/7XzeB.png
[2]: https://i.stack.imgur.com/9tAB8.png
[3]: https://i.stack.imgur.com/MtgXs.png
Try:
Library (maps)
Your_data %>% ggplot (aes(lat, lon)) +
Borders ("states") +
geom_polygon()
I'm trying to create a map of the world and US states, colored by some categorical variable. My idea was to draw the world map, then draw the US state map over it:
library(maps)
library(ggplot2)
library(dplyr)
states_map <- map_data("state")
world_map <- map_data("world")
world_map <- world_map %>%
filter(region != "Antarctica")
states <- c("texas")
world <- c("Alaska",
"Canada",
"France")
world_map$region <- ifelse(world_map$subregion == "Alaska", "Alaska", world_map$region)
world_map$status <- ifelse(world_map$region %in% world, TRUE, FALSE)
states_map$status <- ifelse(states_map$region %in% states, TRUE, FALSE)
ggplot() +
geom_map(aes(map_id = region, fill = status),
map = world_map,
data = world_map,
color = "black") +
geom_map(aes(map_id = region, fill = status),
map = states_map,
data = states_map,
color = "black") +
expand_limits(x = world_map$long, y = world_map$lat)
But none of France is colored correctly, and most of Canada isn't (some of the islands are):
Any idea where I'm going wrong here? Note that if you remove Canada from "world", all goes well...
The problem is that the country data set you are using is ill suited for this purpose. It contains many overlapping shapes which causes issues with colouring, and countries are poorly labeled
France proper is actually group 558, and has no data in it's region or sub-region column:
ggplot() +
geom_map(aes(map_id = region, fill = status),
map = world_map[world_map$group == 558,],
data = world_map[world_map$group == 558,],
color = "black") +
expand_limits(x = world_map$long, y = world_map$lat)
This is also under a few shapes that describe Europe, which hide it's colouring even if you directly colour group 558.
map_data("world2") does not seem to have this problem, but the perspective used is different:
I would suggest downloading better documented shapefiles, and using those to map instead.
I am trying to label my polygons by using ggplot in R. I found a topic here on stackoverflow that I think is very close to what I want except with points.
Label points in geom_point
I found some methods online. Now I first need to find the central location of each shape and then I have to put these locations together with the name together. Then link this to the labeling function in geom_text()
ggplot centered names on a map
Since I have been trying for a long time now I decided to ask the question and hope that someone here can give me the final push to what I want. My plotting function:
region_of_interest.fort <- fortify(region_of_interest, region = "score")
region_of_interest.fort$id <- as.numeric(region_of_interest.fort$id)
region_of_interest.fort$id <- region_of_interest.fort$id
region_of_interest.fort1 <- fortify(region_of_interest, region = "GM_NAAM")
region_of_interest.fort1$id <- as.character(region_of_interest.fort1$id)
region_of_interest.fort1$id <- region_of_interest.fort1$id
idList <- unique(region_of_interest.fort1$id)
centroids.df <- as.data.frame(coordinates(region_of_interest))
names(centroids.df) <- c("Longitude", "Latitude")
randomMap.df <- data.frame(id = idList, shading = runif(length(idList)), centroids.df)
ggplot(data = region_of_interest.fort, aes(x = long, y = lat, fill = id, group = group)) +
geom_polygon() +
geom_text(centroids.df, aes(label = id, x = Longitude, y = Latitude)) +
scale_fill_gradient(high = "green", low = "red", guide = "colorbar") +
coord_equal() +
theme() +
ggtitle("Title")
It gives me the error: ggplot2 doesn't know how to deal with data of class uneval
My data
region_of_interest$GM_NAAM
[1] Groningen Haren Ooststellingwerf Assen Aa en Hunze Borger- Odoorn
[7] Noordenveld Westerveld Tynaarlo Midden-Drenthe
415 Levels: 's-Gravenhage 's-Hertogenbosch Aa en Hunze Aalburg Aalsmeer Aalten ... Zwolle
region_of_interest$score
[1] 10 -2 -1 2 -1 -4 -4 -5 0 0
Try something like this?
Get a data frame of the centroids of your polygons from the
original map object.
In the data frame you are plotting, ensure there are columns for
the ID you want to label, and the longitude and latitude of those
centroids.
Use geom_text in ggplot to add the labels.
Based on this example I read a world map, extracting the ISO3 IDs to use as my polygon labels, and make a data frame of countries' ID, population, and longitude and latitude of centroids. I then plot the population data on a world map and add labels at the centroids.
library(rgdal) # used to read world map data
library(rgeos) # to fortify without needing gpclib
library(maptools)
library(ggplot2)
library(scales) # for formatting ggplot scales with commas
# Data from http://thematicmapping.org/downloads/world_borders.php.
# Direct link: http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip
# Unpack and put the files in a dir 'data'
worldMap <- readOGR(dsn="data", layer="TM_WORLD_BORDERS_SIMPL-0.3")
# Change "data" to your path in the above!
worldMap.fort <- fortify(world.map, region = "ISO3")
# Fortifying a map makes the data frame ggplot uses to draw the map outlines.
# "region" or "id" identifies those polygons, and links them to your data.
# Look at head(worldMap#data) to see other choices for id.
# Your data frame needs a column with matching ids to set as the map_id aesthetic in ggplot.
idList <- worldMap#data$ISO3
# "coordinates" extracts centroids of the polygons, in the order listed at worldMap#data
centroids.df <- as.data.frame(coordinates(worldMap))
names(centroids.df) <- c("Longitude", "Latitude") #more sensible column names
# This shapefile contained population data, let's plot it.
popList <- worldMap#data$POP2005
pop.df <- data.frame(id = idList, population = popList, centroids.df)
ggplot(pop.df, aes(map_id = id)) + #"id" is col in your df, not in the map object
geom_map(aes(fill = population), colour= "grey", map = worldMap.fort) +
expand_limits(x = worldMap.fort$long, y = worldMap.fort$lat) +
scale_fill_gradient(high = "red", low = "white", guide = "colorbar", labels = comma) +
geom_text(aes(label = id, x = Longitude, y = Latitude)) + #add labels at centroids
coord_equal(xlim = c(-90,-30), ylim = c(-60, 20)) + #let's view South America
labs(x = "Longitude", y = "Latitude", title = "World Population") +
theme_bw()
Minor technical note: actually coordinates in the sp package doesn't quite find the centroid, but it should usually give a sensible location for a label. Use gCentroid in the rgeos package if you want to label at the true centroid in more complex situations like non-contiguous shapes.
The accepted answer here may work, but the actual question asked specifically notes that there is an error "ggplot2 doesn't know how to deal with data of class uneval."
The reason that it is giving you the error is because the inclusion of centroids.df needs to be a named variable (e.g. accompanied by "data=")
Currently:
ggplot(data = region_of_interest.fort, aes(x = long, y = lat, fill = id, group = group)) +
geom_polygon() +
geom_text(centroids.df, aes(label = id, x = Longitude, y = Latitude)) +
scale_fill_gradient(high = "green", low = "red", guide = "colorbar") +
coord_equal() +
theme() +
ggtitle("Title")
Should be (note: "data=centroids.df"):
ggplot(data = region_of_interest.fort, aes(x = long, y = lat, fill = id, group = group)) +
geom_polygon() +
geom_text(data=centroids.df, aes(label = id, x = Longitude, y = Latitude)) +
scale_fill_gradient(high = "green", low = "red", guide = "colorbar") +
coord_equal() +
theme() +
ggtitle("Title")
This issue was addressed here: How to deal with "data of class uneval" error from ggplot2?