1 Load the data

combined <- import(here::here("data", "clean", "backup", "linelist_combined_20141201.rds"))

2 Simple epidemic curve, default setting

## simple epidemic curve, default setting
ggplot(                     # open the plot
  data = combined,          # open the database
  mapping = aes(
    x = date_onset          # chose variable(s) to include
  )) +
  geom_histogram()          # choose the geometry histogram

3 Simple epidemic curve, binwidth = 1 (each bar = 1 day)

## simple epidemic curve,  binwidth = 1 (each bar = 1 day)
ggplot(
  data = combined,
  mapping = aes(
    x = date_onset
  )) +
  geom_histogram(binwidth = 1) # specify the bin width

4 Simple epidemic curve, binwidth = 7 (each bar = 7 days)

## simple epidemic curve,  binwidth = 7 (each bar = 7 days)
 # but... we don't know when start the bin 
ggplot(
  data = combined,
  mapping = aes(
    x = date_onset
  ))+
  geom_histogram(binwidth = 7) # specify the bin width

5 Simple epidemic curve, using parameter breaks to define the break points for the histogram bins

5.1 Create the break points for the histogram bins

# first date of onset
min(combined$date_onset, na.rm = T)
## [1] "2014-05-06"
# last date of onset
max(combined$date_onset, na.rm = T)
## [1] "2014-11-28"
# first day of the week of the week of the first date of onset
floor_date(min(combined$date_onset, na.rm = T),
           unit = "week",
           week_start = 1)
## [1] "2014-05-05"
# first day of the week of the following week of the last date of onset
ceiling_date(max(combined$date_onset, na.rm = T),
           unit = "week",
           week_start = 1)
## [1] "2014-12-01"
# break points for the histogram bin
 # => sequence of dates with first day of the week of all weeks from the week with the first date of onset to the week with the last date of onset  
ebola_weeks <- 
  seq.Date(
    from = floor_date(min(combined$date_onset, na.rm = T),
                      unit = "week",
                      week_start = 1),
    to   = ceiling_date(max(combined$date_onset, na.rm = T),
                        unit = "week",
                        week_start = 1),
    by   = "weeks"
  )
ebola_weeks
##  [1] "2014-05-05" "2014-05-12" "2014-05-19" "2014-05-26" "2014-06-02"
##  [6] "2014-06-09" "2014-06-16" "2014-06-23" "2014-06-30" "2014-07-07"
## [11] "2014-07-14" "2014-07-21" "2014-07-28" "2014-08-04" "2014-08-11"
## [16] "2014-08-18" "2014-08-25" "2014-09-01" "2014-09-08" "2014-09-15"
## [21] "2014-09-22" "2014-09-29" "2014-10-06" "2014-10-13" "2014-10-20"
## [26] "2014-10-27" "2014-11-03" "2014-11-10" "2014-11-17" "2014-11-24"
## [31] "2014-12-01"

5.2 Making the epicurve

ggplot(
  data = combined,
  mapping = aes(
    x = date_onset
  ))+
  geom_histogram(
    breaks = ebola_weeks # specify the break points for the histogram bins
  )

6 Simple epidemic curve with break points and x-axis with month using scale_x_date

ggplot(
  data = combined,
  mapping = aes(
    x = date_onset
  ))+
  geom_histogram(
    breaks = ebola_weeks
  ) +
scale_x_date(
    date_breaks = "1 months") # specify the labels on the x axis (date), appears every month

7 simple epidemic curve with break points and x-axis with month using scale_x_date

## simple epidemic curve with break points and x-axis with month using scale_x_date
ggplot(
  data = combined,
  mapping = aes(
    x = date_onset
  ))+
  geom_histogram(
    breaks = ebola_weeks
  ) +
  scale_x_date(
    date_breaks = "1 months",  # specify the labels on the x axis (date): a label every month
    date_labels = "%B")        # specify the format of the labels : month name

8 Simple epidemic curve with break points and x-axis with weeks using scale_x_date and label_date_short

## simple epidemic curve with break points and x-axis with weeks using scale_x_date and label_date_short
ggplot(
  data = combined,
  mapping = aes(
    x = date_onset
  ))+
  geom_histogram(
    breaks = ebola_weeks
  ) +
  scale_x_date(
    date_breaks = "1 week",             # specify the labels on the x axis (date): a label every week
    labels = scales::label_date_short() # automatically constructs a short format string sufficient to uniquely identify labels
  )

9 Same as previous changing color of lines around bars and color of fill within bars

## same as previous changing color of lines around bars and color of fill within bars
ggplot(
  data = combined,
  mapping = aes(
    x = date_onset
  ))+
  geom_histogram(
    breaks = ebola_weeks,
    color = "darkblue",     # color of lines around bars
    fill = "lightblue"      # color of fill within bars
  ) +
  scale_x_date(
    date_breaks = "1 months",
    labels = scales::label_date_short()
  )

10 Same as previous adding labels for titles

## same as previous adding labels for titles
ggplot(
  data = combined,
  mapping = aes(
    x = date_onset
  ))+
  geom_histogram(
    breaks = ebola_weeks,
    color = "darkblue",     # color of lines around bars
    fill = "lightblue"      # color of fill within bars
  ) +
  scale_x_date(
    date_breaks = "1 months",
    labels = scales::label_date_short()
    ) +
  labs(x = "Date",                                                  # title of x-axis
       y = "Cases (n)",                                             # title of y-axis
       title = "Cases by week of onset",                            # graphic title
       subtitle = str_glue("Source: MSF data from {Sys.Date()}"))   # graphic sub-title

11 Same as previous with simplify plot background using theme_bw()

## same as previous with simplify plot background using theme_bw()
ggplot(
  data = combined,
  mapping = aes(
    x = date_onset
  ))+
  geom_histogram(
    breaks = ebola_weeks,
    color = "darkblue",     # color of lines around bars
    fill = "lightblue"      # color of fill within bars
  ) +
  scale_x_date(
    date_breaks = "2 weeks",
    labels = scales::label_date_short()
  ) +
  labs(x = "Date",                                                  # title of x-axis
       y = "Cases (n)",                                             # title of y-axis
       title = "Cases by week of onset",                            # graphic title
       subtitle = str_glue("Source: MSF data from {Sys.Date()}"))+   # graphic sub-title
  theme_bw()                # simplify plot background