Last active
June 26, 2019 03:26
-
-
Save rafapereirabr/85259a950474d518dbda59ae92a5e1bc to your computer and use it in GitHub Desktop.
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(data.table) | |
library(ggplot2) | |
library(sf) | |
library(dplyr) | |
library(maps) | |
library(rworldmap) | |
# Visitors data ------------------- | |
df <- fread("C:/Users/rafa/Downloads/users_cities_UrbanDemog_blog_20180625-20190625_all.csv") | |
df$Users <- as.numeric(df$Users) | |
# Visitors location ------------------- | |
w <- maps::world.cities | |
map <- merge.data.frame(w, df, by.x="name", by.y="City") | |
map <- subset(map, Users >0) | |
map <- subset(map, !is.na(Users)) | |
map <- map[order(map$Users),] | |
head(map) | |
# world map ---------------- | |
wmap <- rworldmap::getMap(resolution="low") | |
# small edits | |
wmap <- st_as_sf(wmap) # to sf | |
wmap <- subset(wmap, !(NAME %like% "Antar")) # Remove Antarctica | |
# Plot ---------------- | |
temp <- | |
ggplot() + | |
geom_sf(data=wmap, color="gray98") + | |
geom_point(data=map, aes(x=long, y=lat, size=Users), color="skyblue4", alpha=.2) + #fill=Users, | |
theme_void() + | |
scale_size_continuous(limits=c(1, max(map$Users)), breaks=seq(1, max(map$Users)+140, by=140)) + | |
guides(color= guide_legend(), size=guide_legend()) + | |
labs(size="Number of visitors") + | |
theme( legend.position = c(.2, .08), | |
# legend.position="bottom", | |
legend.direction = "horizontal", | |
legend.box = "horizontal", | |
legend.title.align = 0, | |
legend.key.size = unit(.1, "cm"), | |
legend.title=element_text(size=10), | |
legend.text=element_text(size=8) ) + | |
guides(size = guide_legend(title.position = "top", | |
# hjust = 0.5 centres the title horizontally | |
title.hjust = 0.5, | |
label.position = "bottom")) | |
ggsave( temp, file="visitors.png", dpi = 200, width = 8, height = 4.5) | |
beepr::beep() |
Author
rafapereirabr
commented
Jun 26, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment