I am fairly new to R and to map plotting. I am trying to overlay some network information on a world map. I loaded a shapefile from Natural Earth as a large SpatialPolygonsDataFrame
via readOGR
. I finally simplified the map and converted it to a data frame through the broom
package
I followed https://flowingdata.com/2011/05/11/how-to-map-connections-with-great-circles/ and https://gjabel.wordpress.com/2016/06/15/euro-2016-squads/#more-1622, which extends to ggplot the previous contribution.
After adapting the code by Abel to my data, I tried to plot both the basemap and the arcs between capital cities, computed using the gcIntermediate()
function from the geosphere
package. Nonetheless, despite the very good tutorials, I am stuck.
My code is
# Building Basemap
basemap.110.gilbert <- ggplot(data = shape.110.tidy, aes(x=long, y=lat, group=group)) +
coord_map("gilbert", ylim = c(-60, 70)) +
geom_polygon(aes(x=long, y=lat, group=group), color = "darkgrey", fill = "black") +
theme_classic() +
theme(panel.background = element_rect(fill = "black"),
axis.ticks=element_blank(),
axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank())
# ---------------------------------------------------------------
# (Should) add connection lines between capital cities
for(i in 1:nrow(data.2005.3)){
data.2005.4 <- data.2005.3 %>%
select(cap_lon_o, cap_lat_o, cap_lon_d, cap_lat_d, color_o, stock_od, pop_o, pop_d, gdp_o, gdp_d) %>%
slice(i) %>%
mutate(color_o = adjustcolor(color_o, alpha = 0.6))
data.2005.5 <- as.data.frame(gcIntermediate(p1 = c(data.2005.4$cap_lon_o, data.2005.4$cap_lat_o),
p2 = c(data.2005.4$cap_lon_d, data.2005.4$cap_lat_d),
n = 50, addStartEnd=TRUE)) %>%
rename(long = lon) %>%
#as.data.frame() %>%
as_tibble()
basemap.110.gilbert.2 <- basemap.110.gilbert.2 +
geom_point(data = data.2005.4, aes(x = cap_lon_o, y = cap_lat_o, group = ), inherit.aes = FALSE, color = data.2005.4$color_o ) +
geom_point(data = data.2005.4, aes(x = cap_lon_d, y = cap_lat_d), inherit.aes = FALSE, color = data.2005.4$color_o )
basemap.110.gilbert.3 <- basemap.110.gilbert.3 +
geom_point(data = data.2005.4, aes(x = cap_lon_o, y = cap_lat_o), inherit.aes = FALSE, color = data.2005.4$color_o ) +
geom_point(data = data.2005.4, aes(x = cap_lon_d, y = cap_lat_d), inherit.aes = FALSE, color = data.2005.4$color_o ) +
geom_line(data = data.2005.5, aes(x=long, y=lat), inherit.aes = FALSE, color = data.2005.4$color_o)
} # Closing FOR
# ------------------------------------------------------
gc() # Garbage collection...boh...
basemap.110.gilbert.2 # has capital cities as geom_point()
basemap.110.gilbert.3 # should also have geom_lines() linking capitals
The point is, when I try to plot the first map I have no problem in getting the projection with the capital cities laying over it. When I try to plot the second map, I receive the following error
Error in .f(.x[[i]], ...) : object 'long' not found
Things does not change if I build the basemap with geom_map()
# Using geom_map()
shape.110.tidy$region = shape.110.tidy$id
basemap.110.provater <- ggplot(data=shape.110.tidy, aes(x = long, y = lat, group = group)) +
geom_map(map=shape.110.tidy, aes(map_id=id, fill = "black"), color="#000000", size=0.25) +
geom_polygon(color = "darkgrey", fill = "black") +
coord_map("gilbert") +
theme_classic() +
theme(panel.background = element_rect(fill = "black"),
axis.ticks=element_blank(),
axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.position = "none")
I am stuck, as I do not understand where the problem is. I am sure the error is due to geom_line()
, but I am not able to guess much more. Any ideas for why this might be happening? Alternatively, are you aware of any alternative to do something similar in R?
I cannot disclose the data I am using, but they are structured more or less the same as those made available by Guy Abel in his GitHub page. My code largely comes from my personal adaptation of it. (https://github.com/gjabel/ecmig)
names(data.2005.5)
orcolnames(data.2005.5)
?