filepath <- "data/stds-by-disease-county-year-sex-2001-2021.csv"

std <- read_csv(filepath) %>%
  rename_with(~ tolower(gsub(" ","_",.x,fixed=TRUE))) 
## Rows: 11151 Columns: 10
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): Disease, County, Sex, Annotation Code
## dbl (6): Year, Cases, Population, Rate, Lower 95% CI, Upper 95% CI
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#using same plots as from the ggplot examples last week
std_gg <- std %>%
  mutate(year=as.character(year),
         disease_f=factor(disease,ordered=T,levels=c("Chlamydia","Gonorrhea","Early Syphilis")))

std_gg1 <- std_gg %>%
  filter(county == "California" & disease_f=="Chlamydia" & sex != "Total") %>%
  select(county, disease_f, cases, year, sex)

Introduction

What follows is a set of examples of California STD Surveillance data visualized using plotly package for R. Add another descriptive sentance here.

#simple barchart
plot_ly(
  std_gg1,
  x= ~year,
  y= ~cases,
  color= ~sex,
  type="bar"
  # mode="bar"
) 
#bar chart - side by side vs stacked
plot_ly(
  std_gg1,
  x= ~year,
  y= ~cases,
  color= ~sex,
  type="bar"
) %>%
  layout(barmode="stack")
std_gg2a <- std_gg %>%
  filter(county != "California" & year=="2018" & sex=="Total") %>%
  select(county, disease_f, rate)

#boxplot
plot_ly(
  std_gg2a,
  y=~rate,
  color=~disease_f,
  type="box"
)
#trend over time in alameda county with confidence intervals
std_gg3 <- std_gg %>%
  filter(county=="Alameda" & sex=="Total")

#make a presentable table
plot_ly(
  std_gg3,
  x=~year,
  y=~rate,
  color=~disease_f,
  type="scatter",
  mode="lines",
  colors=c("darkorange","darkcyan","darkslateblue"),
  text = ~paste('Cases: ',cases,'<br>Population: ',population,'<br>Rate: ',rate)
) %>%
  layout(
    title="Alameda County STD Rates, 2001-2018",
    yaxis=list(title="Case Rate per 100,000"),
    xaxis=list(title="Year"),
    paper_bgcolor="azure",
    plot_bgcolor="white"
  )
#add trace example
std_gg3b <- std_gg %>%
  filter(county=="Alameda" & sex=="Total") %>%
  select(year,disease,rate) %>%
  pivot_wider(names_from=disease, values_from=rate)

plot_ly(
  std_gg3b,
  x=~year,
  y=~`Chlamydia`,
  name="Chlamydia",
  type="scatter",
  mode="markers"
) %>%
  add_trace(y=~`Gonorrhea`,name="Gonorrhea",mode="lines")%>%
  add_trace(y=~`Early Syphilis`,name="Early Syphilis",mode="lines+markers") %>%
  layout(yaxis=list(title="Rate per 100,000"))
#subplot example
plot1 <- plot_ly(std_gg3b,
                 x=~year,
                  y=~`Chlamydia`,
                  name="Chlamydia",
                  type="scatter")

plot2 <- plot_ly(std_gg3b,
                 x=~year,
                  y=~`Gonorrhea`,
                  name="Gonorrhea",
                  type="scatter")

subplot(plot1,plot2,nrows=2,shareX=T)
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
subplot(plot1,plot2,shareY=T)
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode