library(dplyr)
library(tidyr)
library(readr)
library(ggplot2)
dir <- read_csv('datafile.csv')
bystate = dir %>% group_by(state) %>% select(hospitalname, state) %>% data.frame
states=bystate$state %>% table %>% data.frame()
colnames(states) = c('State', 'Freq')
abbr <- read_csv('stateabbr.csv')
colnames(abbr)<-abbr[1,]
abbr = abbr[-1,]
abbr = abbr[,-1]
colnames(abbr)=c('State', 'abbreviation')
states = merge(abbr,states, by="State")
states=states %>% arrange(desc(Freq))
colnames(states)= c('state', 'abbreviation', 'numberofhospitals')
ggplot(states, aes(x = reorder(abbreviation, -numberofhospitals), y = numberofhospitals)) + geom_bar(stat = "identity")
