Search code examples
rdataframeggplot2tidyverser-sf

Making a diagram map in r?


This is my dataframe:

df:

Country          Total       lon        lat
United Kingdom   5000       -3.43597    55.37805
China            4000       104.1954    35.86166
France           4000       2.213749    46.22764
Australia        4500       133.7751    -25.2744
Mexico           0          -102.553    23.6345

I am new to R. I am trying to make a map showing travel from many different countries to Mexico. The destination is Mexico, so all the data flow will be in one direction. I don't know how to make the connections between countries. I have used the following codes to make a layout:



world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)
new_df <- read.csv('df.csv')
ggrepel::geom_label_repel

ggplot(data = world) +
  geom_sf()+geom_point(data=new_df, aes(x=lon, y=lat), colour='red')+
  ggrepel::geom_label_repel(data = new_df, 
                            aes(x = lon, y = lat), 
                            label = new_df$Country,
                            size = 2,
                            col = "blue")

Solution

  • You can use geom_curve and use the size argument. You can also use color to augment contrast:

    new_df <- new_df %>% 
      mutate(Total = as.numeric(gsub(",", "", Total)),
             lon2 = lon[Country == "Mexico"],
             lat2 = lat[Country == "Mexico"])
    #          Country   Total         lon       lat     lon2    lat2
    # 1 United Kingdom 7200000   -3.435970  55.37805 -102.553 23.6345
    # 2          China 5359000  104.195400  35.86166 -102.553 23.6345
    # 3         Canada 6016000 -106.347000  56.13037 -102.553 23.6345
    # 4         Brazil 5269000  -51.925300 -14.23500 -102.553 23.6345
    # 5         France 4484000    2.213749  46.22764 -102.553 23.6345
    # 6      Australia 4014000  133.775100 -25.27440 -102.553 23.6345
    # 7         Mexico       0 -102.553000  23.63450 -102.553 23.6345   
    
    
    ggplot(data = world) +
      geom_sf() + 
      geom_curve(data = new_df[-7, ], 
                 aes(x = lon, y = lat, xend = lon2, yend = lat2, size = Total, color = Total),
                 curvature = -0.2, 
                 lineend = "round",
                 arrow = arrow(length = unit(0.1, "cm"))) +
      scale_size(range = c(.5, 3), label = scales::comma) +
      scale_color_distiller(label = scales::comma, 
                            palette = "Reds", 
                            direction = 1) +
      guides(color = guide_legend(), 
             size = guide_legend()) +
      ggrepel::geom_label_repel(data = new_df, aes(x = lon, y = lat), 
                                label = new_df$Country, size = 2, col = "blue") +
      ggthemes::theme_map()
    

    enter image description here


    Data

    new_df <- read.table(header = T, text = "Country          Total       lon        lat
    'United Kingdom'   7,200,000   -3.43597   55.37805
    China            5,359,000   104.1954   35.86166
    Canada           6,016,000   -106.347   56.13037
    Brazil           5,269,000   -51.9253   -14.235
    France           4,484,000   2.213749   46.22764
    Australia        4,014,000   133.7751   -25.2744
    Mexico           0           -102.553   23.6345")