I've been using Rworldmap to create a map that has specific Countries highlighted and points representing those cities. I'd like to add some text next to the city points that says the city name.
I've tried loading ggplot2 and loading in the code for geom text:
geom_text(data = plotcities, aes(lat,long), stat = "identity",
position = "identity", parse = FALSE, check_overlap = TRUE, na.rm = FALSE,
show.legend = FALSE, inherit.aes = TRUE)
All I get back is null or errors.
Here is the general code:
library(rworldmap)
library("ggmap")
library(maptools)
library(maps)
library(RColorBrewer)
data("world.cities")
plotcities <- subset(world.cities,
name %in% c("Cologne", "Chennai", "Denver", "Madrid", "Manila", "San Diego", "Seattle", "Shanghai")
& country.etc %in% c("Germany", "USA", "Spain", "China", "Philippines", "India"))
theCountries <- c("USA",
"CAN", "DEU", "FRA", "IND",
"GBR", "NLD", "ITA",
"CHN", "KOR", "JPN",
"ESP", "PRT", "RUS",
"NOR", "SGP", "AUS",
"CHL", "MEX", "PHL", "RWA",
"JOR", "HND", "PAN", "THA", "DOM",
"ZAF", "TUR", "CHE", "FIN",
"SEN", "BOL", "OMN", "PAK", "CMR", "MUS", "BEL", "MYS",
"UAE", "BRA", "MLI", "MOZ", "NAM", "EGY", "ARG", "UKR", "ZMB", "KEN",
"VNM", "NGA", "DNK", "IRN", "AFG")
# These are the ISO3 names of the countries you'd like to plot
CEAMap <- data.frame( country = c("USA",
"CAN", "DEU", "FRA", "IND",
"GBR", "NLD", "ITA",
"CHN", "KOR", "JPN",
"ESP", "PRT", "RUS",
"NOR", "SGP", "AUS",
"CHL", "MEX", "PHL", "RWA",
"JOR", "HND", "PAN", "THA", "DOM",
"ZAF", "TUR", "CHE", "FIN",
"SEN", "BOL", "OMN", "PAK", "CMR", "MUS", "BEL", "MYS", "UAE", "BRA", "MLI", "MOZ", "NAM", "EGY", "ARG", "UKR", "ZMB", "KEN",
"VNM", "NGA", "DNK", "IRN", "AFG"),
involvement = c(1,
2, 2, 2, 2,
3, 3, 3,
4, 4, 4,
5, 5, 5,
6, 6, 6,
7, 7, 7, 7,
8, 8, 8, 8, 8,
9, 9, 9, 9,
10, 10, 10, 10, 10, 10, 10, 10,
11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
12, 12, 12, 12, 12))
# CEAMap is a data.frame with the ISO3 country names plus a variable to
# merge to the map data
# To get rid of that stupid-ass continent Antarctica, the command is != "Antarctica", necessary to create a subset
CEAcountries <- joinCountryData2Map(CEAMap, joinCode = "ISO3",
nameJoinColumn = "country")
new_world <- subset(CEAcountries, continent != "Antarctica")
colourPalette <- RColorBrewer::brewer.pal(12,'PRGn')
# This will join your CEAcountries data.frame to the country map data
mapCountryData(new_world, nameColumnToPlot="country",
catMethod = "categorical", colourPalette = colourPalette,
mapTitle='CEA Locations',
missingCountryCol = gray(.8), addLegend = FALSE)
#PLOTTING THE CITIES ON THE MAP
points(plotcities$long, plotcities$lat, pch = 25, col = "gold1", bg= "gold3")
#ATTEMPT TO ADD NAMES TO THE CITIES
geom_label_repel(data = plotcities,
aes(long, lat, label = "Chart Data.Region", group="Chart"),
fill = "green",
label.size = NA,
color = 'black',
size = 4,
box.padding = unit(.1, "lines"), point.padding = unit(0.0, "lines"))
I'd like to display on the map the names of the cities highlighted on the map : "Cologne", "Chennai", "Denver", "Madrid", "Manila", "San Diego", "Seattle", "Shanghai" but I get NULL when I try to chain it together
ggplot2
won't be of any use in this case. Use text()
from base plot instead.
mapCountryData(new_world, nameColumnToPlot="country",
catMethod = "categorical", colourPalette = colourPalette,
mapTitle='CEA Locations',
missingCountryCol = gray(.8), addLegend = FALSE)
points(plotcities$long, plotcities$lat, pch = 25, col = "gold1", bg= "gold3")
text(plotcities$long, plotcities$lat, plotcities$name, pos = 3)
You can adjust size, color and the position (to the left, right etc of your city) as you like.