https://github.com/zachcp/piesonamap
draw piecharts on a map with ggplot
https://github.com/zachcp/piesonamap
Last synced: 3 months ago
JSON representation
draw piecharts on a map with ggplot
- Host: GitHub
- URL: https://github.com/zachcp/piesonamap
- Owner: zachcp
- Created: 2016-02-24T18:06:33.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-02-26T17:01:49.000Z (over 9 years ago)
- Last Synced: 2025-02-24T04:13:00.989Z (3 months ago)
- Language: R
- Size: 146 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
README
## piesonamap
draw piecharts on map
## Inspiration
http://stackoverflow.com/questions/10368180/plotting-pie-graphs-on-map-in-ggplot
## Usage
```r
library(piesonamap)
data(nycounty)
data(pops)library(dplyr)
library(ggtree)# Get Centroids of NY counties using the sp package.
getLabelPoint <- function(county) {sp::Polygon(county[c('long', 'lat')])@labpt}
df <- map_data('county', 'new york') # NY region county data
centroids <- by(df, df$subregion, getLabelPoint) # Returns list
centroids <- do.call("rbind.data.frame", centroids) # Convert to Data Frame
names(centroids) <- c('long', 'lat') # Appropriate Header
centroids$region <- rownames(centroids)# process NY data and merge with centroids
ny %>%
select(region,long,lat,white,black,hispanic,asian,other) %>%
group_by(region) %>%
slice(1:1) %>%
reshape2::melt(id.vars = c("region","long","lat")) -> nydata
nydata %>%
select(-lat, -long) %>%
left_join(centroids, by=c("region"="region")) %>%
select(region, long, lat, variable, value) -> nydata2# draw the pies on a map
gg <- piesonmap(data=ny,map=ny,piedata=nydata2,locationdata=NULL, circlesize = 0.05)
gg```
