0
votes

I can produce a map of Europe and the bubble plot that would layer on top of the map but I do not know how to merge these together successfully. Will someone please help me do this? :)

Here is the code I have used to create the two separate components:

    library(cowplot)
    library(googleway)
    library(ggplot2)
    library(ggrepel)
    library(ggspatial)
    library(sf)
    library(rnaturalearth)
    library(rnaturalearthdata)    
    library("ggplot2")
    theme_set(theme_bw())
    library("sf")
    library("rnaturalearth")
    library("rnaturalearthdata")
    
    world <- ne_countries(scale = "medium", returnclass = "sf")
    class(world)
     
    ## Produces the map of Europe ##
    ggplot(data = world) +
    geom_sf() +
    coord_sf(xlim = c(-15, 55), ylim = c(34, 71), expand = FALSE)
   
             
    cities <- read.csv("cities1_R.csv")
    df <- cities
             
    df$Country <- as.factor(df$Country)
             
    head(df[ , c("City", "Country", "Latitude", "Longitude", "No..of.Samples")], 28)
             
    ## Produces Bubbles plot ##               
    ggplot(df, aes(x = Longitude, y = Latitude)) + 
    geom_polygon(df, aes(x= Longitude, y= Latitude, group=group), fill="grey", alpha=0.3)
    geom_point(aes(color = Country, size = No..of.Samples), alpha = 0.9) +
    scale_color_manual(values = c("#FF3300", "#0066CC", "#FFFF00", "#CC0066", "#9933CC", 
    "#00FFFF", "#FF9900", "#000000", "#00CC00", 
    "#999999", "#FF6666", "#FF3399", "#669933", 
    "#000099", "#6666FF", "#003300", "#66FF33", "#660066", "#FF99CC", 
    "#333333", "#CC33FF", "#CC6699", "#663300", "#330033", "#666600", "#CCFF00", "#CC9900", 
    "#990033")) +
    scale_size(breaks = c(0, 25, 50, 75, 100, 125, 150, 175), range = c(0.5, 12))  # Adjust 
    the range of points size

The size of the bubbles is dependent on the no. of samples in the cities data. This is the table: cities1_R.csv file showing the data in a table format

City Country Latitude Longitude No..of.Samples
1 AMSTERDAM NETHERLANDS 52.37403 4.88969 22
2 BARI ITALY 41.11148 3.53390 196
3 BUDAPEST HUNGARY 47.49801 19.03991
4 LONDON UNITED KINGDOM 51.50853 -0.12574
5 BERLIN GERMANY 52.52437 13.41053
6 COPENHAGEN DENMARK 55.67594 12.56553
7 LISBOA PORTUGAL 38.71667 -9.13333
8 WARSZAW POLAND 52.22977 21.01178
9 DUBLIN IRELAND 53.33306 -6.24889
10 LJUBLJANA SLOVENIA 46.05108 14.50513
11 BRUXELLES BELGIUM 50.85045 4.34878
12 PARIS FRANCE 48.85341 2.34880
13 WIEN AUSTRIA 48.20849 16.37208
14 ASTURIAS SPAIN 43.36140 -5.85930
15 ATHENS GREECE 37.98380 23.72750
16 STOCKHOLM SWEDEN 59.32930 18.06860
17 SOFIA BULGARIA 42.69770 23.32190
18 ZAGREB CROATIA 45.81500 15.98190
19 NICOSIA CYPRUS 35.18560 33.38230
20 PRAGUE CZECH REPUBLIC 50.07550 14.43780
21 TALLINN ESTONIA 59.43700 24.75360
22 HELSINKI FINLAND 60.16990 24.93840
23 RIGA LATVIA 56.94960 24.10520
24 VILNIUS LITHUANIA 54.68720 25.27970
25 LUXEMBOURG CITY LUXEMBOURG 49.81530 6.12960
26 VALLETTA MALTA 35.89890 14.51460
27 BUCHAREST ROMANIA 44.42680 26.10250
28 BRATISLAVA SLOVAKIA 48.14860 17.10770

This is the map of Europe I have produced: Map of Europe

This is the bubble plot I have produced: Bubble ggplot

If someone knows how to merge the two ggplots that would be amazing. I tried using the geom_polygon function but I couldn't get it to work. Thank you in advance!

1
Could you please make it into a reproducible example? If you add a hard-coded version of your dataframe loaded from the CSV with just a couple of rows it would make it a lot easier for anyone to run your code,Leonardo Viotti
Is that what you meant? I have edited the post^.Em3
Not quite, but I added a quick example in my answer.Leonardo Viotti

1 Answers

0
votes

I added a rough version of a repex with just 3 cities, but this should work with your entire data.

library(cowplot)
library(googleway)
library(ggplot2)
library(ggrepel)
library(ggspatial)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)    
library("ggplot2")
theme_set(theme_bw())
library("sf")
library("rnaturalearth")
library("rnaturalearthdata")

x = "1 AMSTERDAM NETHERLANDS 52.37403 4.88969 22

7 LISBOA PORTUGAL 38.71667 -9.13333 12

3 BUDAPEST HUNGARY 47.49801 19.03991 0"


df <- read.table(text=x, 
                 header = F,
                 col.names=c('idx', 'City', 'Country', 'Latitude',  'Longitude', 'No..of.Samples'))

world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)

## Produces the map of Europe ##
ggplot(data = world) +
  geom_sf() +
  geom_point(df, 
             mapping = aes(x= Longitude, y= Latitude,  size = No..of.Samples, color = Country), 
             fill="grey", alpha=0.3) + 
  coord_sf(xlim = c(-15, 55), ylim = c(34, 71), expand = FALSE)

It outputs:

enter image description here