Saïd Maanan
Saïd Maanan

Reputation: 697

Scatterpie pie plot: circles not properly positioned over map

I am trying to create a map where I show the amount and category of Exports in every European country, using a scatterpie plot. This is the data I am trying to represent:

   Country           A            B           C            D            E            F           G       Total
1       FR 48208727011 129696846358 34574295963  99154544367  87056475894 104059261659   391086898  50.3141238
2       BE 30008344795 130642251666 27315419464  48966420544  51351672841  57686707705   875915760  34.6846733
3       NL 53815652300 126965690773 52604259051 164935573324  43089183110  79607329056   516212340  52.1533900
4       DE 79643366705 285793555191 66579801287 230961697801 160598853461 167790359814 13590821673 100.4958456
5       IT 35306881277 124880125091 31042897909  65051137874  44481779280  65707113992   307508636  36.6777444
6       UK  4190569134  14226329164  4343541388   8299777138   7863823675   8191378024   177728913   4.7293147
7       IE  8049979989  25547263228  3324685081  15609577840  18293778082  13299495081   284077060   8.4408856
8       DK 10844794488  22366273732  3669934507  20904821209   8871184551  17364886109  1104100358   8.5125995
9       EL  5548998459  14199041489  9684405892   6969942717   2877634605   8740624663     9513713   4.8030162
10      PT  9302893141  19921174761  5742487970  12183620710   9794141959  10889202370    59025653   6.7892547
11      ES 29087706350  79136960848 26777114009  45807156391  43316950993  54577475375   225619825  27.8928984
12      LU  2103037221   5485541709  1274451840   3165573258   3448812873   2685200517    23828895   1.8186446
13      SE 14297019504  32367817406 10023929115  31082425639  18504243058  21520786963   251825497  12.8048047
14      FI  4368941438  17924135085  6424290821  13268574752   7679357024   7759601514    87932852   5.7512833
15      AT 11108739001  47969735941  8282060600  36180768764  20761302493  26060191499   319396555  15.0682195
16      MT   529547453    748570490   789405002    772157398    939286493    808546088     1179489   0.4588692
17      EE  1387220092   4797469841  1253135597   3127037067   1483571375   2251847940   315884341   1.4616166
18      LV  2714038229   4237027490   958962478   3158721396   1479290893   2931423023    89667330   1.5569131
19      LT  3408636288   8761053696  3263941940   5534705815   2630113004   4477086678   348351748   2.8423889
20      PL 17264039729  70678231411 11723435712  53284056901  28214023352  41438947683   319384835  22.2922120
21      CZ  7664643659  38573705210  5359209173  54059163460  20745595183  22423687496   216009863  14.9042014
22      SK  4193310193  17229538594  3771900263  19251595573  18415022178  10092362707   163300267   7.3117030
23      HU  5067726212  26282833327  5807291521  31406620462  16576651093  12918544146   456905984   9.8516573
24      RO  7210065674  24768518425  3986448288  20279628790  10274528929  13490373296   213856837   8.0223420
25      BG  3364866564  11098005470  2490021719   5767532283   2282959524   4540599434   289425842   2.9833411
26      SI  2226481542  11769625979  2186097710   5986840366   6169533307   8453642146    32927930   3.6825149
27      HR  2664219116   7204053277  2281750708   4155735739   2094082503   4970586651    14826478   2.3385254
28      CY   847756088   1467939342   983937418    824244195   1900124484   1375465594    47109886   0.7446577

Using the following code:

library(giscoR)

borders <- gisco_get_countries(
  epsg = "3035",
  year = "2020",
  resolution = "3",
  country = idf$Country
)

merged <- merge(borders,
  idf,
  by.x = "CNTR_ID",
  by.y = "Country",
  all.x = TRUE
)

library(tidyverse)

symbol_pos <- st_centroid(merged, of_largest_polygon = TRUE)

separate_coords = symbol_pos %>% mutate(lat = unlist(map(symbol_pos$geometry, 1)), long = unlist(map(symbol_pos$geometry, 2)))

sympos = data.frame(Country = separate_coords$CNTR_ID, lat = separate_coords$lat, long = separate_coords$long)

merged <- merge(merged,
            sympos,
            by.x = "CNTR_ID",
            by.y = "Country",
            all.x = TRUE
)

ggplot() +
  geom_sf(data = merged, size = 0.1) +
  geom_scatterpie(data = merged, aes(x = long, y = lat, r = Total), cols = LETTERS[1:7])+
  coord_sf(xlim = c(2377294, 6500000), ylim = c(1413597, 5228510))

And it gives me this error:

Error in rowSums(data[, cols]) : 'x' must be numeric

I am trying to create a map similar to this one:

enter image description here

And I would be grateful if someone can provide some hint as to how to fix the error. Thanks.

Edit: below is the dput(idf) output:

structure(list(Country = c("FR", "BE", "NL", "DE", "IT", "UK", 
"IE", "DK", "EL", "PT", "ES", "LU", "SE", "FI", "AT", "MT", "EE", 
"LV", "LT", "PL", "CZ", "SK", "HU", "RO", "BG", "SI", "HR", "CY"
), A = c(48208727011, 30008344795, 53815652300, 79643366705, 
35306881277, 4190569134, 8049979989, 10844794488, 5548998459, 
9302893141, 29087706350, 2103037221, 14297019504, 4368941438, 
11108739001, 529547453, 1387220092, 2714038229, 3408636288, 
17264039729, 
7664643659, 4193310193, 5067726212, 7210065674, 3364866564, 
2226481542, 
2664219116, 847756088), B = c(129696846358, 130642251666, 
126965690773, 
285793555191, 124880125091, 14226329164, 25547263228, 
22366273732, 
14199041489, 19921174761, 79136960848, 5485541709, 32367817406, 
17924135085, 47969735941, 748570490, 4797469841, 4237027490, 
8761053696, 70678231411, 38573705210, 17229538594, 26282833327, 
24768518425, 11098005470, 11769625979, 7204053277, 1467939342
), C = c(34574295963, 27315419464, 52604259051, 66579801287, 
31042897909, 4343541388, 3324685081, 3669934507, 9684405892, 
5742487970, 26777114009, 1274451840, 10023929115, 6424290821, 
8282060600, 789405002, 1253135597, 958962478, 3263941940, 
11723435712, 
5359209173, 3771900263, 5807291521, 3986448288, 2490021719, 
2186097710, 
2281750708, 983937418), D = c(99154544367, 48966420544, 
164935573324, 
230961697801, 65051137874, 8299777138, 15609577840, 20904821209, 
6969942717, 12183620710, 45807156391, 3165573258, 31082425639, 
13268574752, 36180768764, 772157398, 3127037067, 3158721396, 
5534705815, 53284056901, 54059163460, 19251595573, 31406620462, 
20279628790, 5767532283, 5986840366, 4155735739, 824244195), 
E = c(87056475894, 51351672841, 43089183110, 160598853461, 
44481779280, 7863823675, 18293778082, 8871184551, 2877634605, 
9794141959, 43316950993, 3448812873, 18504243058, 7679357024, 
20761302493, 939286493, 1483571375, 1479290893, 2630113004, 
28214023352, 20745595183, 18415022178, 16576651093, 10274528929, 
2282959524, 6169533307, 2094082503, 1900124484), F = 
c(104059261659, 
57686707705, 79607329056, 167790359814, 65707113992, 8191378024, 
13299495081, 17364886109, 8740624663, 10889202370, 54577475375, 
2685200517, 21520786963, 7759601514, 26060191499, 808546088, 
2251847940, 2931423023, 4477086678, 41438947683, 22423687496, 
10092362707, 12918544146, 13490373296, 4540599434, 8453642146, 
4970586651, 1375465594), G = c(391086898, 875915760, 516212340, 
13590821673, 307508636, 177728913, 284077060, 1104100358, 
9513713, 59025653, 225619825, 23828895, 251825497, 87932852, 
319396555, 1179489, 315884341, 89667330, 348351748, 319384835, 
216009863, 163300267, 456905984, 213856837, 289425842, 32927930, 
14826478, 47109886), Total = c(50.314123815, 34.6846732775, 
52.1533899954, 100.4958455932, 36.6777444059, 4.7293147436, 
8.4408856361, 8.5125994954, 4.8030161538, 6.7892546564, 
27.8928983791, 
1.8186446313, 12.8048047182, 5.7512833486, 15.0682194853, 
0.4588692413, 1.4616166253, 1.5569130839, 2.8423889169, 
22.2922119623, 
14.9042014044, 7.3117029775, 9.8516572745, 8.0223420239, 
2.9833410836, 3.682514898, 2.3385254472, 0.7446577007)), 
row.names = c(NA, 
-28L), class = "data.frame")

Upvotes: 3

Views: 1366

Answers (1)

lovalery
lovalery

Reputation: 4662

Please find below one possible solution to your request. The main problem was that geom_scatterpie() expects a dataframe and not an sf object. So you need to use as.data.frame() inside geom_scatterpie(). I also took the opportunity to simplify your code a bit.

Reprex

  • Code
library(giscoR)
library(sf)
library(dplyr)
library(ggplot2)
library(scatterpie)


borders <- gisco_get_countries(
  epsg = "3035",
  year = "2020",
  resolution = "3",
  country = idf$Country
)

merged <- merge(borders,
                idf,
                by.x = "CNTR_ID",
                by.y = "Country",
                all.x = TRUE
)


symbol_pos <- st_centroid(merged, of_largest_polygon = TRUE)

sympos <- symbol_pos %>% 
  st_drop_geometry() %>% 
  as.data.frame() %>% 
  cbind(., symbol_pos %>% st_coordinates()) %>% 
  select(CNTR_ID, X, Y) %>% 
  rename(Country = CNTR_ID, long = X, lat = Y)


merged <- merge(merged,
                sympos,
                by.x = "CNTR_ID",
                by.y = "Country",
                all.x = TRUE
)
  • Visualization
ggplot() + 
  geom_sf(data = merged, size = 0.1) + 
  geom_scatterpie(data = as.data.frame(merged), aes(x = long, y = lat, r = Total*2200), cols = LETTERS[1:7]) + 
  coord_sf(xlim = c(2377294, 6500000), ylim = c(1413597, 5228510))

Created on 2022-01-23 by the reprex package (v2.0.1)

Upvotes: 5

Related Questions