Geo-pie plot in R
Geo-pie plots offer a refreshing way to represent data by combining the simplicity of pie charts with the spatial context provided by maps. They can be used effectively in various domains such as demographic analysis or market research to uncover new insights and make informed decisions based on geographic patterns.
In this post, we are going to make a quick geo-pie of the market shares of five companies using fictitious data.
So let’s cook up some data:
set.seed(123)
phones <- data.frame(Country = c("China", "India", "Bangladesh", "Malawi", "United States", "United Kingdom", "Brazil", "Indonesia", "Germany", "Russia", "Australia", "Malaysia", "South Africa", "Canada", "France", "Japan", "Mexico", "Nigeria")) %>%
mutate(apple = round(abs(rnorm(18, 10, 30)), 1),
samsung = round(abs(rnorm(18, 50, 30)), 1),
xiaomi = round(abs(rnorm(18, 100, 30)), 1),
huawei = round(abs(rnorm(18, 150, 30)), 1),
oppo = round(abs(rnorm(18, 200, 30)), 1))
> phones
Country apple samsung xiaomi huawei oppo
1 China 6.8 71.0 116.6 143.2 230.2
2 India 3.1 35.8 98.1 195.5 178.7
3 Bangladesh 56.8 18.0 90.8 103.5 179.4
4 Malawi 12.1 43.5 88.6 167.5 230.8
5 United States 13.9 19.2 79.2 153.7 191.5
6 United Kingdom 61.5 28.1 93.8 156.5 163.4
7 Brazil 23.8 31.2 62.0 161.4 205.4
8 Indonesia 28.0 0.6 165.1 134.9 195.8
9 Germany 10.6 75.1 136.2 140.0 200.2
10 Russia 3.4 54.6 66.3 119.4 211.6
11 Australia 46.7 15.9 87.9 117.8 188.9
12 Malaysia 20.8 87.6 86.0 159.1 219.3
13 South Africa 22.0 62.8 123.4 163.4 193.4
14 Canada 13.3 41.1 97.5 151.6 210.0
15 France 6.7 76.9 107.6 177.7 232.9
16 Japan 63.6 76.3 99.1 211.5 213.1
17 Mexico 24.9 74.6 98.7 135.3 190.2
18 Nigeria 49.0 70.7 141.1 80.7 234.5
Data looks good, now time to get the great joinCountryData2Map function to work from the rworldmap
package:
df <- joinCountryData2Map(phones, joinCode = "NAME", nameJoinColumn = "Country")@data
Now the plot
par(mai = c(0, 0, 0.6, 0), xaxs = "i", yaxs = "i")
mapPies(dF = df,
nameX = "LON",
nameY = "LAT",
nameZs = c("apple", "samsung", "xiaomi", "huawei", "oppo"),
zColours = c("#A2DED0", "#D4A2A2", "#708090", "#FFDAB9", "#000080"),
oceanCol = "#D9E3DA",
landCol = "#F3DCD4",
addSizeLegend = FALSE,
addCatLegend = FALSE,
mapRegion = "world",
xlim = c(-181, 181),
ylim = c(-81, 80))
title(main = "Market share of Top 5 telecom companies", cex = 2)
legend(-180.1516, 70,
legend = c("Apple", "Samsung", "Xiaomi", "Huawei", "Oppo"),
col = c("#A2DED0", "#D4A2A2", "#708090", "#FFDAB9", "#000080"),
pch = 15,
cex = 1.2,
pt.cex = 1.5,
box.lty = 0,
horiz = F,
bg = "#00000002",
x.intersp = 0.3)