library(tidytuesdayR)
library(tidyverse)
library(maptools)
library(data.table)
Loading libraries
Loading data
<- tidytuesdayR::tt_load(2021, week = 16)
tuesdata <- tuesdata$post_offices post_offices
Wrangling
head(post_offices)
<-post_offices%>%filter(established>=1639,established<=2000,
my_df>=1775 ,discontinued<=2002,
discontinued!is.na(stamp_index),
!duration<0,
!is.na(gnis_dist),
!stamp_index==55,
!is.na(county1))%>%
select("duration","gnis_dist","stamp_index")%>%
group_by(stamp_index)%>%
summarize(avg_duration=mean(duration),avg_dist=mean(gnis_dist))
Setting data ready for plotting
library(igraph)
<- cor(t(my_df))
mat <0.995] <- 0
mat[mat# Make an Igraph object from this matrix:
<- graph_from_adjacency_matrix( mat, weighted=T, mode="undirected", diag=F)
network
# Basic chart
plot(network)
# color palette
library(RColorBrewer)
<- brewer.pal(nlevels(as.factor(my_df$stamp_index)), "Set3")
coul
# Map the color to cylinders
<- coul[as.numeric(as.factor(my_df$stamp_index))] my_color
Setting for saving plot
::agg_png(here::here("US_Post_office_space.png"),
raggres = 320, width = 14, height = 8, units = "in")
# plotting ############################################
par(bg="mediumblue", mar=c(1,1,1,1))
set.seed(4)
plot(network,
vertex.label.family="Georgia",
edge.curved=0.08,
edge.width=2,
edge.arrow.size=1,
edge.arrow.width=1,
edge.lty="solid",
vertex.size=12,
vertex.shape=c("raster","sphere"),
vertex.color=my_color,
vertex.label.cex=0.7,
vertex.label.color="blue",
vertex.frame.color="transparent"
)
<- par(family = "Luminari")
op
#### legend and titles ########################
legend(x=1.3, y=0.7,
legend=paste( levels(as.factor(my_df$stamp_index)), "stamp*", sep="*"),
col = coul ,
bty = "n", pch=20 , pt.cex = 2, cex = 1,
text.col="white" , horiz = F)
text(-1.4,1.1,"US Post Offices",col="white", cex=1.5)
text(-1.3,1,"Stamps index by distance",col="white", cex=1.2)
text(-1.1,0.9,"visualization of the distance between offices by different stamps index",col="white", cex=0.8)
text(-1,-1.1,"Viz @fgazzelloni | #TidyTueasday Week 16 | Space Day 14 | DataSource: Harvard Dataverse",col="white", cex=0.8)
par(op)
Imaging
library(png)
library(grid)
library(magick)
<-image_read("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/static/plot_logo.png") %>%
tidy_logoimage_resize("400x400")
<-readPNG("satellite2.png")
mypng2<-readPNG("satellite3.png")
mypng3
grid.raster(tidy_logo, x=0.9, y=0.2, width=.08)
grid.raster(mypng2, x=.09, y=.7, width=.25)
grid.raster(mypng3, x=.9, y=.9, width=.25)
####### final ###################
# dev.off()