Skip to content

Instantly share code, notes, and snippets.

Created October 10, 2017 01:38
Show Gist options
  • Save anonymous/de97a57c8b76f8b6fee0d9c1a21e1ddc to your computer and use it in GitHub Desktop.
Save anonymous/de97a57c8b76f8b6fee0d9c1a21e1ddc to your computer and use it in GitHub Desktop.
#!/usr/bin/env R
library(curl)
library(dplyr)
library(tidyr)
library(ggplot2)
library(cowplot)
library(gridExtra)
# get data
url <- 'https://docs.google.com/spreadsheets/d/e/2PACX-1vR0TLhKqeEWZsJqbIuZ38bLc3vVeUpPx9rYnS1QG9kp4Wgv9rvCRHplf1fDy4rFbmC1CAQoVLVW3NX7/pub?output=tsv'
req <- curl_fetch_memory(url)
# transform columns to uniform types
nigththingsfac <- function(col) factor(as.character(col),c('necessary','is nice','whatever','dont want'))
distmi <- function(col) as.numeric(gsub('[^0-9]','',col))
d <-
read.table(text=rawToChar(req$content),sep="\t",quote="",header=T) %>%
mutate_each(funs(distmi),matches('Hiking.Distance')) %>%
mutate(id=1:n())
#names(d) <- gsub('Where..([A-Za-z]+).*','\\1',names(d),perl=T)
places <-
d %>%
select(matches('Where'),id) %>%
gather(place,rank,matches('Where')) %>%
mutate(place=gsub('Where..([A-Za-z]+).*','\\1',place)) %>%
mutate(rank=factor(as.character(rank),rev(c('Yes!','Okay','If everyone else is','Nope!'))))
p.d <- ggplot(places) + aes(fill=place,x=as.numeric(rank)) + geom_density(alpha=.9)
p.b <- ggplot(places) + aes(fill=place,x=place,y=as.numeric(rank)) + geom_boxplot() +geom_jitter(height=0,color='white',shape=21,width=.1)
p.byid <-
ggplot(places)+
aes(y=place,x=id,fill=rank) +
geom_tile() +
theme(axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.y = element_text(angle = 45, hjust = 1),
legend.position='bottom')+
labs(fill='',x='',y='') +
guides(fill=guide_legend(nrow=2,byrow=TRUE)) +
scale_fill_manual(values=c('red','yellow','lightgreen','green'))
plc.smry <-
places %>%
group_by(place) %>%
filter( !any(rank=='Nope!')) %>%
summarise(avg=mean(as.numeric(rank))) %>%
arrange(-avg)
p <-
plot_grid(nrow=2,
p.b + ylab(''),
plot_grid(ncol=2,
p.d + theme(legend.position='none'),
p.byid
))
print(p)
ggsave(p,file='hike1014_pref.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment