1

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)

4
  • What is the output of names(data.2005.5) or colnames(data.2005.5)?
    – rcs
    Mar 22, 2019 at 10:11
  • 1
    Please don't paste a large amount of code, especially if we can't run it because we don't have the data. Do two things: simplify your code to the minimum thing that shows the problem, and create a simple shareable dataset that shows the problem. Often doing these two things alone will lead you to solve the problem yourself.
    – Spacedman
    Mar 22, 2019 at 10:52
  • @rcs the names of the columns are 'long' and 'lat' respectively. That is why I cannot figure out where the problem is. Mar 22, 2019 at 11:47
  • @Spacedman I will try Mar 22, 2019 at 11:48

1 Answer 1

1

I found a solution on my own (thank you for your suggestion @spacedman).

The problem was due to the way I defined the basemap aestetics. Initially, I was defining the map aesthetics in ggplot()

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()

Rather, they should be defined in geom_polygon, i.e.

basemap.110.gilbert <- ggplot(data = shape.110.tidy) + 
  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())

Not the answer you're looking for? Browse other questions tagged or ask your own question.