Skip to content

Instantly share code, notes, and snippets.

@jshannon75
Created November 15, 2019 20:49
Show Gist options
  • Save jshannon75/9be17b48c764a2f87ae1a432199ec98f to your computer and use it in GitHub Desktop.
Save jshannon75/9be17b48c764a2f87ae1a432199ec98f to your computer and use it in GitHub Desktop.
Creating a WordCloud from OpenStreetMap in rstats using wordcloud2
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