Labelling points on world map in ggplot2 [duplicate] - r

This question already has answers here:
Show the value of a geom_point using geom_text
(2 answers)
Closed 5 years ago.
I have created a world map with the use of the ggplot2 library.
I am attempting to label the two cities (Shanghai and Sao Paulo) respectively through using labels in ggplot2. However, when I try to add labels I get the error message:
Warning: Ignoring unknown aesthetics: labels
Error: geom_text requires the following missing aesthetics: x, y, label
Here is the full code:
require(maps)
require(mapdata)
library(ggplot2)
countries = c("Sao Paulo","Shanghai")
global <- map_data("world")
ggplot() + geom_polygon(data = global, aes(x=long, y = lat, group =
group)) +
coord_fixed(1.3)
ggplot() +
geom_polygon(data = global, aes(x=long, y = lat, group = group),
fill = NA, color = "red") +
coord_fixed(1.3)
gg1 <- ggplot() +
geom_polygon(data = global, aes(x=long, y = lat, group = group),
fill = "green", color = "blue") +
coord_fixed(1.3)
gg1
labs <- data.frame(
long = c(-46.625290,121.4580600),
lat = c(-23.533773,31.2222200),
stringsAsFactors = FALSE
)
gg1 +
geom_point(data = labs, aes(x = long, y = lat), color = "red", size
= 5) + ggtitle("World Map") + geom_text(aes(labels=countries),vjust=0,
colour="red")
Clearly, I'm using ggplot wrong in some way but can't figure out how.

labs$countries <- countries
gg1 +
geom_point(data=labs, aes(long, lat), colour="red", size=5) +
ggtitle("World Map") +
geom_text(data=labs, aes(long, lat, label=countries))

Related

Ggplot map issue when adding fill

Does anyone have any ideas why this map glitches and how to fix it? I plotted it without my sample size variable and it worked fine. However, as soon as I added the fill all these lines appeared.
And
That's all the code I used:
map <- geojson_read("https://martinjc.github.io/UK-GeoJSON/json/sco/topo_lad.json", what = "sp")
map_fortified <- tidy(spdf, reigion="code")
ggplot() +
geom_polygon(data = map_fortified, aes( x = long, y = lat, group = group), fill="white", color="grey") +
theme_void() +
coord_map()
data2<-read.csv("location.csv", header = TRUE)
head(data2)
data2$id<-as.factor(data2$id)
map_fortified$id<-as.factor(map_fortified$id)
total <- merge(data2,spdf_fortified,by="id")
ggplot() +
geom_polygon(data = total, aes(fill = sample, x = long, y = lat, group = group)) +
theme_void() +
coord_map()

Change fill colour of polygon with ggplot2

I have a simple question which I've asked myself many times already. When I am plotting one or more polygons in R using ggplot2, how can I actually change the filling colour? I do not only want to change the outline of the polygon, but the whole thing.
In my first simple plots (ea_map and hb_map) it works fine to assign the colours "yellow" and "purple", however in my final plot "ea_hb_map" (which I've pictured below), the colours are set back to default.
ea <- readOGR("C:/Users/BASELINE/Eastern Arctic/Summer/2010_EA_S/cis_SGRDREA_20100628_pl_a.shp")
hb <- readOGR("C:/Users/BASELINE/Hudson Bay/Summer/2010_HB_S/cis_SGRDRHB_20100628_pl_a.shp")
ea_map <- ggplot() +
geom_polygon(data=ea, aes(x = long, y = lat, group = group), fill = "red")
plot(ea_map)
hb_map <- ggplot() +
geom_polygon(data=hb, aes(x = long, y = lat, group = group), fill = "purple")
plot(hb_map)
ea_df <- tidy(ea)
hb_df <- tidy(hb)
eastern_arctic_map <- ea_map +
geom_sf(data=world, fill = "antiquewhite1") +
coord_sf(xlim = c(-115, -50), ylim = c(50,83), expand = FALSE)+
scale_y_continuous(breaks = c(50, 60, 70, 80)) +
scale_x_continuous(breaks = c(-50, -70, -90, -110)) +
geom_polygon(data = ea_df, aes(x=long, y=lat, group=group, fill="Eastern Arctic"), alpha=0.4) +
labs(fill = "",
x = "lon",
y = "lat") +
theme_grey(base_size = 9) +
theme(legend.key.size = unit(0.8,"line"))
print(eastern_arctic_map)
ea_hb_map <- eastern_arctic_map +
geom_polygon(data=hb_df, aes(x = long, y = lat, group = group, fill = "Hudson Bay"), alpha=0.4)
print(ea_hb_map)

How do I combine a raster dataframe with a shapefile to create a map of species richness?

I want to plot the raster plot/dataframe on top of the shapefile but I keep getting various errors depending on how I write the code.
I've tried using a + with the finished objects, landmap+critmapped, I've tried adding the codes together and plotting both as data but that didn't work:
I've tried the following, as well as other things...
Thank you for any help/direction.
critmapped<-ggplot(df, aes(x, y, fill = layer)) +
geom_raster() +
scale_fill_viridis_c(na.value = "white") +
labs(fill = "Count") +
theme_minimal() + ggplot() + geom_path(data = land_df, aes(x = long, y = lat, group = group), color = 'black', fill = 'green')
#Error: Don't know how to add ggplot() to a plot
critmapped+landmap2
#Error: Don't know how to add landmap2 to a plot
#The shapefile code
require(rgdal)
land <- readOGR(dsn = "C:/Users/tjef631/Desktop/R Stats/Data/NE_10m_full",
layer = "ne_10m_land")
land_df<-fortify(land)
names(land_df)
landmap<-ggplot() + geom_path(data = land_df, aes(x = long, y = lat, group
= group),
color = 'black', fill = 'green')
landmap
#the raster/dataframe code
critmapped<-ggplot(df, aes(x, y, fill = layer)) +
geom_raster() +
scale_fill_viridis_c(na.value = "white") +
labs(fill = "Count") +
theme_minimal()
critmapped

Is it possible to overlay a scatterplot on map using ggplot?

I would like to draw a map of French departements (ie districts in France) and plot points on the map. I use ggplot2 to draw the map but when I want to add points on the map, R returns an error which says that it cannot find 'group'.
library("ggplot2")
library("ggmap")
# load the contour of French departements
wg <- read.csv("data/departements.csv")
metropoles <- c("Paris", "Rennes", "Rouen", "Lille", "Marseille")
geo <- geocode(metropoles)
met <- data.frame(ville = metropoles, geo)
map <- ggplot(data = wg, aes(x = long, y = lat, group = group)) +
geom_polygon() +
scale_x_continuous(limits = c(-7,10)) +
scale_y_continuous(limits = c(40,53)) +
coord_map() +
theme(axis.text = element_blank(), axis.title = element_blank())
map + geom_point(data = met, aes(x = lon, y = lat))
For replication, you can find the raw data here and the R program here
#Roland had the good answer. The problem is that I had defined group as a general argument.
ggplot() +
geom_polygon(data = wg, aes(x = long, y = lat, group = group)) +
scale_x_continuous(limits = c(-7,10)) +
scale_y_continuous(limits = c(40,53)) +
coord_map() +
theme(axis.text = element_blank(), axis.title = element_blank())
geom_point(data = met, aes(x = lon, y = lat))

How to draw ggmap with two different administrative boundaries?

I am wondering how to draw a map using get_map and ggmap of any federal country (i.e. a country with provinces and counties). Any country, other than the US would be great. To make it look nice, fill the geom_polygon of counties (any fill), and provinces are empty polygons, only with its contours. So, basically, it is two overlapping ggmaps.
You can get the shapefiles here:
https://www.dropbox.com/s/4nl685t860x1e8r/municipios_br.zip
rm(list = ls())
library(ggplot2)
library(rgdal)
library(ggmap)
# READ SHAPEFILE OF BOUNDARIES
Map <- readShapePoly("municipios_br.shp")
head(as.data.frame(Map))
Map = gBuffer(Map, width=0, byid=TRUE)
MapC <- fortify(Map, region="CODIGO_MUN") # municipalities
MapP <- fortify(Map, region="CODIGO_UF") # state boundaries
MapC$test <- 1
MapP$test <- 1
MapC <- Map[order(MapC$order),]
MapP <- MapP[order(MapP$order),]
The following code produces counties boundaries:
google.map <- get_map(location = 'Brazil', zoom=4,maptype="terrain")
m0 <- ggmap(google.map)
m1 <- m0 + geom_polygon(color = 'grey90', size = .01, aes(x=long, y=lat, group=group, fill=as.factor(test)), data=MapC, alpha=.6)
m1 + guides(fill=FALSE) + scale_fill_manual(values=c("red"))
Now, provinces:
m2 <- m0 + geom_polygon(color = 'grey50', size = .1, aes(x=long, y=lat, group=group, fill=as.factor(test)), data=MapP, alpha=.9)
m2 + guides(fill=FALSE) + scale_fill_manual(values=c(NA))
How to make the two work together?
You could also get your maps from e.g. GADM:
library(raster)
adm1 <- getData('GADM', country='HUN', level=0)
adm2 <- getData('GADM', country='HUN', level=1)
And let us fortify those for ggplot usage:
library(ggplot2)
fadm1 = fortify(adm1)
fadm2 = fortify(adm2)
And add as many layers and geoms as you wish:
ggplot(fadm1, aes(x = long, y = lat, group = group)) + geom_path() +
geom_polygon(data = fadm2, aes(x = long, y = lat),
fill = "green", alpha = 0.5) +
geom_path(data = fadm2, aes(x = long, y = lat), color = "blue") +
theme_bw()
Resulting in:
Update: combining your two layers mentioned in the updated questions
m0 + geom_polygon(size = .01,
aes(x = long, y = lat, group = group, fill = as.factor('red')),
data = MapC,
alpha = .6) +
geom_path(color = 'grey50', size = .1, aes(x = long, y = lat, group = group),
data=MapP, alpha=.9) +
guides(fill=FALSE)

Resources