Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visualization with {ggplot2} #92

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 84 additions & 1 deletion episodes/describe-cases.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ base::plot(dialy_incidence_data) +


```{r, message=FALSE, warning=FALSE}

# Plot weekly incidence data

base::plot(weekly_incidence_data) +
Expand Down Expand Up @@ -254,6 +253,90 @@ print(peak)
This example demonstrates how to estimate the peak time using the `estimate_peak()` function at $95%$
confidence interval and using 100 bootstrap samples.

## Visulaziantion with ggplot2


`{incidence2}` produces basic plots for epicurves, but additional work is required to create well-annotated graphs. However, using the `{ggplot2}` package, you can generate more sophisticated and better-annotated epicurves.
`{ggplot2}` is a comprehensive package with many functionalities. However, we will focus on three key elements for producing epicurves: histogram plots, scaling date axes and their labels, and general plot theme annotation.
The example below demonstrates how to configure these three elements for a simple `{incidence2}` object.

```{r, message=FALSE, warning=FALSE}
breaks <- seq.Date(
from = min(as.Date(dialy_incidence_data$date_index,
na.rm = TRUE
)),
to = as.Date(max(dialy_incidence_data$date_index,
na.rm = TRUE
)),
by = 1
)

ggplot2::ggplot(data = dialy_incidence_data) +
geom_histogram(
mapping = aes(
x = as.Date(date_index),
y = count
),
stat = "identity",
color = "blue",
width = 1
) +
theme_minimal() + # simple theme
theme(
plot.title = element_text(face = "bold", hjust = 0.5),
plot.caption = element_text(face = "italic", hjust = 0),
axis.title = element_text(face = "bold"),
axis.text.x = element_text(angle = 45)
) +
labs(
x = "Date", # x-label
y = "Number of cases", # y-label,
title = "Daily outbreak cases", # title
subtitle = "subtitle", # subtitle
caption = "informative caption"
) +
scale_x_date(
breaks = breaks,
label = scales::label_date_short()
)
```

Use the `group` option in the mapping function to visualize an epicurve with different groups. If there is more than one grouping factor, use the `facet_wrap()` option, as demonstrated in the example below:

```{r, message=FALSE, warning=FALSE}
ggplot2::ggplot(data = dialy_incidence_data_2) +
geom_histogram(
mapping = aes(
x = as.Date(date_index),
y = count,
group = sex,
fill = sex
),
stat = "identity"
) +
theme_minimal() + # simple theme
theme(
plot.title = element_text(face = "bold", hjust = 0.5),
plot.caption = element_text(face = "italic", hjust = 0),
axis.title = element_text(face = "bold"),
axis.text.x = element_text(angle = 45)
) +
labs(
x = "Date", # x-label
y = "Number of cases", # y-label,
title = "Daily outbreak cases", # title
subtitle = "subtitle", # subtitle
caption = "informative caption"
) +
facet_wrap(~sex) +
scale_x_date(
breaks = breaks,
label = scales::label_date_short()
)
```





::::::::::::::::::::::::::::::::::::: challenge
Expand Down
Loading