I plotted some data points on a map. The Map is the bounty King in Washington. I plotted them on the map successfully. My dataset consists of a column called prices. Their values range in between 0-800000.
I created a new column, called MapColor to print the value either as red or blue based on the price value. If price> 400000 red, else blue.
Now when plotting the points in the map, if MapColor is red I need to map it as red points and if not black. This is how I tried it. But the colors are plotting black only. This is what I tried
long <- c(47.5112,47.7210 ,47.3684)
lat <- c(-122.257, -122.319, -122.031)
price <- c(287655,456355,662500,234563)
House <- data.frame(long, lat, price)
House$MapColor <- ifelse(House$price >=400000, "red", "black")
col <- as.character(House$MapColor)
states <- map_data("state")
wa_df <- states %>%
filter(region == "washington", subregion == 'king')
counties <- map_data("county")
wa_county <- counties %>%
filter(region == "washington")
wa_base <-
ggplot(data = wa_df,
mapping = aes(x = long, y = lat, group = group)) + geom_point(data = House,aes(x = long, y = lat),size = 0.5,inherit.aes = FALSE) +
coord_fixed(1.3) +scale_color_manual(values=col)+
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
Please let me know if this is what you had in mind.
I think long and lat for those example points were reversed. I set the colors as either red or blue as per the description.
It maps the state polygon, then county, then adds the points using color = House$MapColor.
library(ggplot2)
library(ggmap)
lat <- c(47.5112, 47.7210, 47.3684)
long <- c(-122.257, -122.319, -122.031)
price <- c(287655, 456355, 662500)
House <- data.frame(long, lat, price)
House$MapColor <- ifelse(House$price >= 400000, "red", "blue")
wa_df <- map_data("state") %>%
filter(region == "washington")
wa_county <- map_data("county") %>%
filter(region == "washington")
ggplot(data = wa_df, mapping = aes(x = long, y = lat, group = group))+
geom_polygon(color = "black", fill = NA)+
geom_polygon(data = wa_county, fill = NA, color = "black")+
geom_point(data = House, mapping = aes(x = long, y = lat), color = House$MapColor)+
coord_fixed(1.3)+
theme_nothing()
Related
I'm struggling to find a way to reproject the co-ordinates of my dataframe so I can plot them into a different projection. When I do all points collapse towards 0,0. so I take it the map itself is projecting towards my desired projection but the data aren't in the correct crs or don't have a crs?
The dataframe is all from the palaeobiology database, I downloaded all Palaeognathae occurance data and it contains, Genus, species, long, lat, paleolat, paleolong, cc.
Here is a link to my dataset: https://github.com/aamirrr-m/palaeognathae/blob/3ed0d951ba5968be67fdc95793ed3c52c8025386/Palaeognathae_Occurance_dataRevised.csv
**so far I can create a map with the mollweide projection without any datapoints **
(https://i.stack.imgur.com/lJQA5.png)
without any projection it works fine and it outputs this:
(https://i.stack.imgur.com/jxght.png)
when I attempt to add the data this is what returns:
(https://i.stack.imgur.com/6820D.png)
(Dooes not let me post images bc i have less than 10 rep)
so I tried adding coord_sf with the projection [using coord_sf(crs = "+proj=moll")] or something similar. this was added to the final ggplot at the bottom of the code block.
I do know some form of transformation must be done first, just need some help in knowing how to do it.
my code is as follows:
dput(ratites)
library(maps)
library(dplyr)
library(ggplot2)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
#install.packages("ggthemes")
library(ggthemes)
library(mapproj)
library(tidyverse)
library(sp)
#projection of earth for plotting####
world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)
attach(ratites)
#Mollweide projection
world %>%
st_transform(crs = "+proj=moll") %>%
ggplot() +
geom_sf() +
geom_sf()
theme_map()
#Preparing the data####
#this cleasn the data using the tidyverse pipe function selecting
#only the rows and columns you want to use
Ratite_C <- ratites %>%
select(accepted_name, Longitude, latitude, early_interval, cc)
#subsetting the data####
#they have all now been separated by time bin
Cret <- subset(Ratite_C, early_interval == "Cretaceous")
Paleo <- subset(Ratite_C, early_interval == "Paleocene")
Eoc <- subset(Ratite_C, early_interval == "Eocene")
Oligo <- subset(Ratite_C, early_interval == "Oligocene")
Mioc <- subset(Ratite_C, early_interval == "Miocene")
Plio <- subset(Ratite_C, early_interval == "Pliocene")
#these two have been combined to show recent ratite biogeography
Pleis.Holo <- subset(Ratite_C, early_interval == "Pleistocene" | early_interval == "Holocene")
#replotting the map data
R.Smap <- ggplot() +
geom_sf(data = world, color = "black", fill = "white", ) +
geom_point(data = Cret, aes( x = Longitude, y = latitude), size = 3,
shape = 23, fill = "orange") +
geom_point(data = Paleo, aes( x = Longitude, y = latitude), size = 3,
shape = 21, fill = "lightblue") +
geom_point(data = Eoc, aes( x = Longitude, y = latitude), size = 3,
shape = 21, fill = "goldenrod4") +
geom_point(data = Oligo, aes( x = Longitude, y = latitude), size = 3,
shape = 21, fill = "yellow") +
geom_point(data = Mioc, aes( x = Longitude, y = latitude), size = 3,
shape = 21, fill = "purple") +
geom_point(data = Plio, aes( x = Longitude, y = latitude), size = 3,
shape = 21, fill = "green") +
geom_point(data = Pleis.Holo, aes( x = Longitude, y = latitude), size = 2,
shape = 21, fill = "cyan") +
theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),
panel.background = element_rect(fill = 'azure2'))`
R.Smap
I am trying to incorporate labels of provinces, and cities with labels and dots into a South African map.I can get a map with provincial subdivisions from a spdf file, from website https://gadm.org/download_country_v3.html
SA1spdf <- readRDS("gadm36_ZAF_1_sp.rds")
And I am trying to use the information in the slot:
SA1spdf#data#ADM1_EN
that contains the label of the 9 provinces
I want to place the labels in the center of the polygons, if possible, so I got the centroids of the 9 provinces:
centroids_SAdf <- as.data.frame(coordinates(SA1spdf))
and change names V1 and V2 to Longitude and Latitude
names(centroids_SAdf) <- c("Longitude", "Latitude")
Also I want to add a few cities to the plot:
coordinates_cities <- data.frame(city = c("Cape Town", "Durban", "Johannesburg", "Port Elizabeth"),lat = c(-33.930, -29.870, -26.190, -33.960 ), lon = c(18.460, 30.990, 28.040,25.590 )
but I cannot link all these together.
I can get a map with:
qtm(SA1spdf, fill = "white")
Using maptools and the following codes all return error messages
qtm(SA1spdf, fill = "white") + tm_text("SA1spdf$data#ADMN1_EN")
tm_shape(SA1spdf) + tm_polygons() + tm_fill(col = "white")
I dont get a white filled map, its filled with grey colour
the following codes return error messages:
tm_shape(SA1spdf) + tm_polygons() + tm_text(c("Eastern Cape","Free State" , "Gauteng","KwaZulu-Natal", "Limpopo","Mpumalanga","North West","Nothern Cape","Western Cape"))
ggplot(SA1spdf, aes(x = lon, y = lat), group = group) + geom_polygon(fill = "white") + geom_point(data = coordinates_cities, aes(x = lat, y = lon)) + geom_text(data= coordinate_cities, aes(label = city))
library(sp)
library(ggplot2)
library(tmap)
library(rgeos)
I'll use the same object SA1spdf you created with data from ZAF as sp object from GADM
coordinate_cities <- data.frame(
city = c("Cape Town", "Durban", "Johannesburg", "Port Elizabeth"),
lat = c(-33.930, -29.870, -26.190, -33.960),
long = c(18.460, 30.990, 28.040,25.590))
base plot
plot(SA1spdf)
points(coordinate_cities$lon, coordinate_cities$lat, pch = 16, col = "red", cex = 1)
text(coordinate_cities$lon, coordinate_cities$lat, coordinate_cities$city, adj = c(0, 0), cex = 0.7)
title(main = "South Africa", sub = "Main cities")
ggplot2
sa_df <- fortify(SA1spdf)
#> Regions defined for each Polygons
ggplot(sa_df, aes(long, lat)) +
geom_polygon(aes(group = group), fill = "white", colour = "black") +
geom_point(data = coordinate_cities, aes(x = long, y = lat), colour = "red") +
geom_text(data = coordinate_cities, aes(label = city), vjust = -1) +
coord_equal()
tmap
# convert coordinate_cities to sp object
sa_cities_spdf <- SpatialPointsDataFrame(coords = coordinate_cities[, c("long", "lat")],
data = coordinate_cities[, "city", drop = FALSE],
proj4string = CRS("+proj=longlat +datum=WGS84"))
tm_shape(SA1spdf) +
tm_borders() +
tm_layout(main.title = "South Africa regions") +
tm_text("NAME_1", size = 0.7) +
tm_shape(sa_cities_spdf) + tm_symbols(size = 0.5, col = "red")
Created on 2021-06-17 by the reprex package (v0.3.0)
As the title says.
When I mapping a continuous value to a map it works well.
When I mapping a discrete value to a map it works also well.
But when I combine the two layers an error occur:
Error: Discrete value supplied to continuous scale
Reproducible code was here:
library(scatterpie)
library(tidyverse)
library(geosphere)
us <- map_data('state') %>% as_tibble()
n = length(unique(us$region))
# creat fake mapping data
temperature_data <- tibble(region = unique(us$region),
temp = rnorm(n = n))
coords <- us %>% select(long, lat, region) %>% distinct(region, .keep_all = T)
category_data <- tibble(region = unique(us$region),
cat_1 = sample(1:100, size = n),
cat_2 = sample(1:100, size = n),
cat_3 = sample(1:100, size = n)) %>% left_join(coords)
us <- left_join(us, temperature_data)
p <- ggplot(us, aes(long, lat))
# mapping temperautre value to map
p + geom_map(map = us, aes(map_id = region, fill = temp), color = 'grey')
# mapping pie chart to map
p +
geom_map(map = us, aes(map_id = region), color = 'grey') +
geom_scatterpie(data = category_data,
aes(long, lat),
cols = c("cat_1", "cat_2", "cat_3"),
alpha = 0.5)
# mapping temperautre and pie chart simultaneously
# ERROR OCCUR
# Error: Discrete value supplied to continuous scale
p + geom_map(map = us, aes(map_id = region, fill = temp), color = 'grey') +
geom_scatterpie(data = category_data,
aes(long, lat),
cols = c("cat_1", "cat_2", "cat_3"),
alpha = 0.5)
I am trying to plot out a base map of Illinois by counties.
Libraries I had loaded:
library(ggplot2)
library(maps)
library(ggmap)
library(mapdata)
This is my code:
states <- map_data("state")
IL <- subset(states, region %in% c("illinois"))
counties <- map_data("county")
IL_county <- subset(counties, region == "illinois")
il_base <- ggplot(data = IL, mapping = aes(x = long, y = lat)) +
coord_fixed(1.3) +
geom_polygon(color = "black", fill = NA) +
theme_nothing()
il_base
il_base +
geom_polygon(data = IL_county, fill = NA, color = "black") +
geom_polygon(color = "black", fill = NA)
The il_base plot is fine, it shows a basic outline of the state. However, as soon as I add geom_polygon to this it maps the counties out like this:
And this is NOT what the counties of IL look like. What did I do wrong here?
I solved the problem by modifying the base plot to:
# Add group
il_base <- ggplot(data = IL, mapping = aes(x = long, y = lat, group = group)) +
coord_fixed(1.3) +
geom_polygon(color = "black", fill = NA) +
theme_nothing()
I have a map with 60 towns to be plotted.
I want to display the name of the towns but it is too cluttered. Is there a way I could number them and display the names as the legend like below?
I have tried "textxy" function from "calibrate" package but it displays only on the map.
Thanks!
Use the package ggmap. It allows you to get a base map and use ggplot functions to plot coordinates on top of the map. Here is a reproducible example:
library(ggmap)
library(dplyr)
# Get the base map
nevada <- get_map(location = 'nevada', maptype = "satellite", source = "google", zoom = 6)
# View the basemap
ggmap(nevada)
# Create data frame with cities and lat/long and an index
cities <- data.frame(city = c("Las Vegas", "Caliente", "Elko"),
lat = c(36.169941, 37.614965, 40.832421),
long = c(-115.13983, -114.511938, -115.763123))
cities <- cities %>% mutate(index = seq.int(nrow(cities)))
# Map with legend, colored by city
ggmap(nevada) +
geom_point(data = cities, aes(x = long, y = lat, color = paste(index, city)), cex = 3) +
labs(color = "cities") +
geom_text(data = cities, aes(x = long, y = lat, label = index), hjust = 2, color = "white")
If you want all the cities to be colored the same, use fill = city instead of color = city and specify the color (e.g. color = "red") outside of aes().
# Map with legend, all cities same color
ggmap(nevada) +
geom_point(data = cities, aes(x = long, y = lat, fill = city), cex = 3, color = "red") +
labs(fill = "cities") +
geom_text(data = cities, aes(x = long, y = lat, label = index), hjust = 2, color = "white")