Scatter Pie plot

set.seed(123)
long <- rnorm(50, sd=100)
lat <- rnorm(50, sd=50)
d <- data.frame(long=long, lat=lat)
d <- with(d, d[abs(long) < 150 & abs(lat) < 70,])
n <- nrow(d)
d$region <- factor(1:n)
d$A <- abs(rnorm(n, sd=1))
d$B <- abs(rnorm(n, sd=2))
d$C <- abs(rnorm(n, sd=3))
d$D <- abs(rnorm(n, sd=4))
d[1, 4:7] <- d[1, 4:7] * 3
head(d)
##          long        lat region          A        B        C        D
## 1  -56.047565  12.665926      1 2.13121969 8.663359 3.928711 8.676792
## 2  -23.017749  -1.427338      2 0.25688371 1.403569 1.375096 4.945092
## 4    7.050839  68.430114      3 0.24669188 0.524395 3.189978 5.138863
## 5   12.928774 -11.288549      4 0.34754260 3.144288 3.789556 2.295894
## 8 -126.506123  29.230687      5 0.95161857 3.029335 1.048951 2.471943
## 9  -68.685285   6.192712      6 0.04502772 3.203072 2.596539 4.439393
ggplot() + geom_scatterpie(aes(x=long, y=lat, group=region), data=d,
                           cols=LETTERS[1:4]) + coord_equal()

d$radius <- 6 * abs(rnorm(n))
p <- ggplot() + geom_scatterpie(aes(x=long, y=lat, group=region, r=radius), data=d,
                                cols=LETTERS[1:4], color=NA) + coord_equal()
p + geom_scatterpie_legend(d$radius, x=-140, y=-70)

The geom_scatterpie is especially useful for visualizing data on a map.

world <- map_data('world')
p <- ggplot(world, aes(long, lat)) +
    geom_map(map=world, aes(map_id=region), fill=NA, color="black") +
    coord_quickmap()
p + geom_scatterpie(aes(x=long, y=lat, group=region, r=radius),
                    data=d, cols=LETTERS[1:4], color=NA, alpha=.8) +
    geom_scatterpie_legend(d$radius, x=-160, y=-55)

p + geom_scatterpie(aes(x=long, y=lat, group=region, r=radius),
                    data=d, cols=LETTERS[1:4], color=NA, alpha=.8) +
    geom_scatterpie_legend(d$radius, x=-160, y=-55, n=3, labeller=function(x) 1000*x^2)

Session info

Here is the output of sessionInfo() on the system on which this document was compiled:

## R version 4.5.0 (2025-04-11 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows 11 x64 (build 26100)
## 
## Matrix products: default
##   LAPACK version 3.12.1
## 
## locale:
## [1] LC_COLLATE=C                               
## [2] LC_CTYPE=Chinese (Simplified)_China.utf8   
## [3] LC_MONETARY=Chinese (Simplified)_China.utf8
## [4] LC_NUMERIC=C                               
## [5] LC_TIME=Chinese (Simplified)_China.utf8    
## 
## time zone: Asia/Shanghai
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] scatterpie_0.2.5 ggplot2_3.5.2   
## 
## loaded via a namespace (and not attached):
##  [1] gtable_0.3.6       jsonlite_2.0.0     dplyr_1.1.4        compiler_4.5.0    
##  [5] maps_3.4.3         tidyselect_1.2.1   Rcpp_1.0.14        tidyr_1.3.1       
##  [9] jquerylib_0.1.4    ggfun_0.1.8        scales_1.4.0       yaml_2.3.10       
## [13] fastmap_1.2.0      R6_2.6.1           labeling_0.4.3     generics_0.1.4    
## [17] knitr_1.50         yulab.utils_0.2.0  MASS_7.3-65        polyclip_1.10-7   
## [21] tibble_3.3.0       bslib_0.9.0        pillar_1.10.2      RColorBrewer_1.1-3
## [25] rlang_1.1.6        cachem_1.1.0       xfun_0.52          fs_1.6.6          
## [29] sass_0.4.10        cli_3.6.5          withr_3.0.2        magrittr_2.0.3    
## [33] tweenr_2.0.3       digest_0.6.37      grid_4.5.0         ggforce_0.4.2     
## [37] lifecycle_1.0.4    vctrs_0.6.5        evaluate_1.0.3     glue_1.8.0        
## [41] farver_2.1.2       prettydoc_0.4.1    purrr_1.0.4        rmarkdown_2.29    
## [45] tools_4.5.0        pkgconfig_2.0.3    htmltools_0.5.8.1