Scatter Pie plot
set.seed(123)
<- rnorm(50, sd=100)
long <- rnorm(50, sd=50)
lat <- data.frame(long=long, lat=lat)
d <- with(d, d[abs(long) < 150 & abs(lat) < 70,])
d <- nrow(d)
n $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))
d1, 4:7] <- d[1, 4:7] * 3
d[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()
$radius <- 6 * abs(rnorm(n))
d<- ggplot() + geom_scatterpie(aes(x=long, y=lat, group=region, r=radius), data=d,
p cols=LETTERS[1:4], color=NA) + coord_equal()
+ geom_scatterpie_legend(d$radius, x=-140, y=-70) p
The geom_scatterpie
is especially useful for visualizing data on a map.
<- map_data('world')
world <- ggplot(world, aes(long, lat)) +
p geom_map(map=world, aes(map_id=region), fill=NA, color="black") +
coord_quickmap()
+ geom_scatterpie(aes(x=long, y=lat, group=region, r=radius),
p data=d, cols=LETTERS[1:4], color=NA, alpha=.8) +
geom_scatterpie_legend(d$radius, x=-160, y=-55)
+ geom_scatterpie(aes(x=long, y=lat, group=region, r=radius),
p 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.0.5 (2021-03-31)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Arch Linux
##
## Matrix products: default
## BLAS: /usr/lib/libblas.so.3.9.0
## LAPACK: /usr/lib/liblapack.so.3.9.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] scatterpie_0.1.6 ggplot2_3.3.3
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.6 highr_0.9 BiocManager_1.30.12
## [4] bslib_0.2.4 compiler_4.0.5 pillar_1.6.0
## [7] jquerylib_0.1.3 prettydoc_0.4.1 tools_4.0.5
## [10] digest_0.6.27 jsonlite_1.7.2 evaluate_0.14
## [13] lifecycle_1.0.0 tibble_3.1.1 gtable_0.3.0
## [16] pkgconfig_2.0.3 rlang_0.4.10 DBI_1.1.1
## [19] rvcheck_0.1.8 yaml_2.2.1 xfun_0.22
## [22] withr_2.4.2 stringr_1.4.0 dplyr_1.0.5
## [25] knitr_1.32 maps_3.3.0 generics_0.1.0
## [28] sass_0.3.1 vctrs_0.3.7 grid_4.0.5
## [31] tidyselect_1.1.0 glue_1.4.2 R6_2.5.0
## [34] fansi_0.4.2 rmarkdown_2.7 polyclip_1.10-0
## [37] tidyr_1.1.3 farver_2.1.0 tweenr_1.0.2
## [40] purrr_0.3.4 magrittr_2.0.1 MASS_7.3-53.1
## [43] scales_1.1.1 htmltools_0.5.1.1 ellipsis_0.3.1
## [46] assertthat_0.2.1 ggforce_0.3.3 colorspace_2.0-0
## [49] labeling_0.4.2 utf8_1.2.1 stringi_1.5.3
## [52] munsell_0.5.0 crayon_1.4.1