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)
Session info
Here is the output of sessionInfo()
on the system on which this document was compiled:
## R version 3.3.2 (2016-10-31)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: OS X El Capitan 10.11.6
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets base
##
## other attached packages:
## [1] maps_3.1.1 scatterpie_0.0.2 ggplot2_2.2.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_0.12.8 knitr_1.15.1 magrittr_1.5 MASS_7.3-45
## [5] munsell_0.4.3 colorspace_1.3-1 R6_2.2.0 dplyr_0.5.0
## [9] stringr_1.1.0 plyr_1.8.4 tools_3.3.2 grid_3.3.2
## [13] gtable_0.2.0 ggforce_0.1.0 DBI_0.5-1 htmltools_0.3.5
## [17] yaml_2.1.14 lazyeval_0.2.0 rprojroot_1.1 digest_0.6.10
## [21] assertthat_0.1 tibble_1.2 tidyr_0.6.0 tweenr_0.1.5
## [25] prettydoc_0.2.0 evaluate_0.10 rmarkdown_1.2 labeling_0.3
## [29] stringi_1.1.2 methods_3.3.2 scales_0.4.1 backports_1.0.4