Skip to content

Instantly share code, notes, and snippets.

@mossmatters
Created August 25, 2021 14:10
Show Gist options
  • Save mossmatters/dbc4020f5c63f78e0e5b68fa6a0129eb to your computer and use it in GitHub Desktop.
Save mossmatters/dbc4020f5c63f78e0e5b68fa6a0129eb to your computer and use it in GitHub Desktop.
library(jsonlite)
library(readxl)
library(dplyr)
library(ggplot2)
#define the source data location and get a JSON with the assetIDs
url = 'https://beta.healthdata.gov/api/views/gqxm-d9w9'
document <- fromJSON(txt=url)
files = document[["metadata"]]$attachments
excels = files[endsWith(files$filename,".xlsx"),]
#Function for downloading data, but only if it's not already there
dataget = function(json_data){
base_url = "https://beta.healthdata.gov/api/views/gqxm-d9w9/files/%s"
dest = sprintf("~/Dropbox/lbbcovid/data/%s",json_data[3])
if (!file.exists(dest)){
download.file(sprintf(base_url,json_data[2]),destfile = dest)
}
}
#download the data with the function (only if it's not already there)
apply(excels,1,dataget)
excel.files = list.files("data",pattern = "\\.xlsx$")
lubbock.list = vector(mode="list",length = length(excel.files))
for (f in 1:length(excel.files)){
date.data = read_excel(sprintf("data/%s",excel.files[f]),sheet="Counties",skip=1)
my.date = as.Date(strsplit(excel.files[f],'_')[[1]][4],"%Y%m%d")
lubbock.date = date.data %>% filter(.[1] == "Lubbock County, TX")
lubbock.date$Date = my.date
lubbock.list[[f]] = lubbock.date
}
names(lubbock.list[[f]])
column_title = "% staffed adult ICU beds occupied by COVID-19 patient"
multiplier = 100
lubbock.icu = data.frame(matrix(NA,nrow=length(excel.files),ncol=2))
for (f in 1:length(excel.files)){
if(column_title %in% colnames(lubbock.list[[f]])){
pct.icu.occupied = lubbock.list[[f]][column_title]
} else{
pct.icu.occupied = "NA"
}
lubbock.icu[f,1]=lubbock.list[[f]]$Date
lubbock.icu[f,2]=pct.icu.occupied
}
names(lubbock.icu) = c("Date","Value")
class(lubbock.icu$Date) = "Date"
class(lubbock.icu$Value) = "numeric"
lubbock.icu$Value = lubbock.icu$Value*multiplier
g = ggplot(lubbock.icu,aes(x=Date,y=Value)) + geom_line() +
scale_x_date(date_labels="%b %y",date_breaks ="1 month") +
ylab(column_title)+
labs (title = "Lubbock County, Texas",
caption= "data: beta.health.gov"
)+
theme_bw()+
theme(axis.title.x = element_blank(),
axis.title.y = element_text(size=15),
plot.title = element_text(size=15),
plot.caption = element_text(size=15))
g
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment