Created
November 15, 2019 20:49
-
-
Save jshannon75/6b6b2515387264e79b26668a1445d889 to your computer and use it in GitHub Desktop.
Creating a WordCloud from OpenStreetMap in rstats using wordcloud2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(osmdata) | |
library(sf) | |
library(tigris) | |
library(wordcloud2) | |
library(tmap) | |
#Load city outline | |
city="Atlanta" | |
ga_place<-places("13",class="sf") | |
place<-ga_place %>% | |
filter(NAME==city) %>% | |
st_transform(4326) | |
#plot(place) | |
##Load osm points | |
#Key list at https://wiki.openstreetmap.org/wiki/Category:Keys | |
bbox_place<-st_bbox(place) | |
spots<-opq(bbox=bbox_place) %>% | |
add_osm_feature(key="cuisine") %>% | |
osmdata_sf() | |
#Select just points in the city | |
spots1<-spots$osm_points %>% | |
st_transform(4326) %>% | |
st_intersection(place) %>% | |
filter(is.na(name)==FALSE) | |
#Count points and select top 60 | |
spots_count<-spots1 %>% | |
st_set_geometry(NULL) %>% | |
count(name,name="freq") %>% | |
rename(word=name) %>% | |
top_n(60) | |
#Plot the map--saved to PDF and imported into Illustrator | |
tm_shape(place)+ | |
tm_polygons(col="#916666")+ | |
tm_layout(frame=FALSE)+ | |
tm_shape(spots1)+tm_dots(size=0.1) | |
#Create plot--used a screenshot with the map in Illustrator | |
wordplot<-letterCloud(spots_count,word="A",letterFont="") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment