ggplot2

Author

Michael L

library(tidyverse)
library(ggthemes)
library(ggsci)

df <- read_csv('data/emergency.csv')

df <- df %>% 
  mutate(
    infection = factor(infection, levels = 0:1, labels = c('No', 'Yes')),
    sex = factor(sex, levels = 0:1, labels = c('Female', 'Male')),
    child = factor(child, levels = 0:2, labels = c('A', 'B', 'C')),
    encephalopathy = factor(encephalopathy, levels = 0:1, labels = c('No', 'Yes')),
    ascites = factor(ascites, levels = 0:1, labels = c('No', 'Yes')),
    death = factor(death, levels = 0:1, labels = c('Alive', 'Died'))
  )

My First Dot Plot

ggplot(data = df, mapping = aes(x = age)) +
  geom_dotplot(method = 'histodot', binwidth = 1)

ggplot(data = df, mapping = aes(x = age)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_light()

ggplot(data = df, mapping = aes(x = age)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_bw()

ggplot(data = df, mapping = aes(x = age)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_minimal()

ggplot(data = df, mapping = aes(x = age)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_dark()

ggplot(data = df, mapping = aes(x = age)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_stata()

ggplot(data = df, mapping = aes(x = age)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_wsj()

ggplot(data = df, mapping = aes(x = age)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_fivethirtyeight()

Modifying ggplot themes

ggplot(data = df, mapping = aes(x = age)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_light() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  )

Adding color to dotplot

ggplot(data = df, mapping = aes(x = age, fill = infection)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_light() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  )

ggplot(data = df, mapping = aes(x = age)) +
  geom_dotplot(method = 'histodot', binwidth = 1, mapping = aes(fill = infection)) +
  theme_light() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  )

Modifying the colors

ggplot(data = df, mapping = aes(x = age, fill = infection)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_light() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  ) +
  scale_fill_manual(values = c('black', '#de8410'))

ggplot(data = df, mapping = aes(x = age, fill = infection)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_light() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  ) +
  scale_fill_brewer(palette = 'Set3')

ggplot(data = df, mapping = aes(x = age, fill = infection)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_light() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  ) +
  scale_fill_brewer(palette = 'Set2')

ggplot(data = df, mapping = aes(x = age, fill = infection)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_light() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  ) +
  scale_fill_brewer(palette = 'Set1')

ggplot(data = df, mapping = aes(x = age, fill = infection)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_light() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  ) +
  scale_fill_brewer(palette = 'Paired')

ggplot(data = df, mapping = aes(x = age, fill = infection)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_light() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  ) +
  scale_fill_nejm()

ggplot(data = df, mapping = aes(x = age, fill = infection)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_light() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  ) +
  scale_fill_rickandmorty()

ggplot panels

ggplot(data = df, mapping = aes(x = age, fill = infection)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_light() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  ) +
  scale_fill_brewer(palette = 'Set3') +
  facet_grid(.~infection)

ggplot(data = df, mapping = aes(x = age, fill = infection)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_light() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  ) +
  scale_fill_brewer(palette = 'Set3') +
  facet_grid(infection~.)

ggplot(data = df, mapping = aes(x = age, fill = infection)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_light() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    legend.position = 'none'
  ) +
  scale_fill_brewer(palette = 'Set3') +
  facet_grid(infection~.)

ggplot(data = df, mapping = aes(x = age, fill = infection)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_light() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    legend.position = 'left'
  ) +
  scale_fill_brewer(palette = 'Set3') +
  facet_grid(infection~.)

ggplot(data = df, mapping = aes(x = age, fill = infection)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_light() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    legend.position = 'top'
  ) +
  scale_fill_brewer(palette = 'Set3') +
  facet_grid(infection~.)

ggplot(data = df, mapping = aes(x = age, fill = infection)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_light() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    legend.position = 'bottom'
  ) +
  scale_fill_brewer(palette = 'Set3') +
  facet_grid(infection~.)

modifying x axis scale

ggplot(data = df, mapping = aes(x = age, fill = infection)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_light() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    legend.position = 'bottom'
  ) +
  scale_fill_brewer(palette = 'Set3') +
  facet_grid(infection~.) +
  scale_x_continuous(breaks = c(20, 50, 55, 62, 71, 80))

ggplot(data = df, mapping = aes(x = age, fill = infection)) +
  geom_dotplot(method = 'histodot', binwidth = 1) +
  theme_light() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    legend.position = 'bottom'
  ) +
  scale_fill_brewer(palette = 'Set3') +
  facet_grid(infection~.) +
  scale_x_continuous(breaks = seq(20, 80, 2))

Vertical Dot Plot

ggplot(data = df, mapping = aes(x = infection, y = age, fill = infection)) +
  geom_dotplot(method = 'histodot', 
               binwidth = 1,
               binaxis = 'y',
               stackdir = 'center') +
  stat_summary(fun = median, geom = 'crossbar')

final_dotplot <- ggplot(data = df, mapping = aes(x = infection, y = age, fill = infection)) +
  geom_dotplot(method = 'histodot', 
               binwidth = 1,
               binaxis = 'y',
               stackdir = 'center') +
  stat_summary(fun = median, geom = 'crossbar', width = .25) +
  theme_bw() +
  labs(x = 'Infection Status', 
       y = 'Age (Years)', 
       title = 'Distribution of Age by Infection Status',
       subtitle = 'This is a subtitle',
       caption = 'This is a caption',
       tag = 'B') +
  scale_y_continuous(breaks = seq(20, 80, 10)) +
  theme(legend.position = 'none',
        axis.title = element_text(face = 'bold', size = 15),
        plot.title = element_text(face = 'bold', size = 10, hjust = .5))

final_dotplot

saving my dot plot

ggsave('figures/myfirst_dotplot.jpg', final_dotplot, width = 7, height = 5)

ggsave('figures/myfirst_dotplot_hd.jpg', final_dotplot, width = 7, height = 5, dpi = 1200)

ggsave('figures/myfirst_dotplot_lossless.pdf', final_dotplot, width = 7, height = 5)

ggsave('figures/myfirst_dotplot_lossless.svg', final_dotplot, width = 7, height = 5)

Modifying figures in qmd

final_dotplot

Histograms

ggplot(data = df, mapping = aes(x = age)) +
  geom_histogram(binwidth = 5) + 
  scale_y_continuous(breaks = seq(0, 30, 5)) +
  theme_clean() +
  labs(x = 'Age (year)', y = 'Count') +
  theme(
    axis.title = element_text(size = 15)
  )

Density Histogram

ggplot(data = df, mapping = aes(x = age, y = after_stat(density))) +
  geom_histogram(binwidth = 5) +
  theme_clean() +
  labs(x = 'Age (year)', y = 'Density') +
  theme(
    axis.title = element_text(size = 15)
  )

Density Curve Histogram

ggplot(data = df, mapping = aes(x = age)) +
  geom_density() +
  theme_clean() +
  labs(x = 'Age (year)', y = 'Count') +
  theme(
    axis.title = element_text(size = 15)
  )

Histogram with colors

ggplot(data = df, mapping = aes(x = age, fill = infection)) +
  geom_histogram(binwidth = 5) + 
  scale_y_continuous(breaks = seq(0, 30, 5)) +
  theme_clean() +
  labs(x = 'Age (year)', y = 'Count') +
  theme(
    axis.title = element_text(size = 15)
  )

ggplot(data = df, mapping = aes(x = age, fill = infection)) +
  geom_histogram(binwidth = 5, breaks = seq(15, 85, 5)) + 
  scale_x_continuous(breaks = seq(15, 85, 5)) +
  scale_y_continuous(breaks = seq(0, 30, 5)) +
  labs(x = 'Age (year)', y = 'Count', fill = 'Infection Status') +
  theme(
    axis.title = element_text(size = 15),
    strip.text = element_text(size = 15)
  ) +
  facet_grid(~infection, labeller = labeller(infection = c('No' = 'No Infection', 'Yes' = 'Yes Infection'))) +
  scale_fill_discrete(labels = c('No' = 'No Infection', 'Yes' = 'Yes Infection'))

Histogram with extra dimensions

ggplot(data = df, mapping = aes(x = age, fill = infection)) +
  geom_histogram(binwidth = 5) +
  scale_y_continuous(breaks = seq(0, 30, 5)) +
  labs(x = 'Age (year)', y = 'Count', fill = 'Infection Status') +
  theme(axis.title = element_text(size = 15),
        strip.text = element_text(size = 15, face = 'bold')) +
  facet_grid(death ~ infection + encephalopathy,
             labeller = labeller(
               infection = c('No' = 'No Infection', 'Yes' = 'Yes Infection'),
               encephalopathy = c('No' = 'No encephalopathy', 'Yes' = 'Yes encephalopathy'),
               death = c('Alive' = 'Did not die', 'Died' = 'Yes did die')
             )) +
  scale_fill_discrete(labels = c('No' = 'No Infection', 'Yes' = 'Yes Infection'))

Boxplots

ggplot(df, aes(y = age, x = 1)) +
  stat_boxplot(geom = 'errorbar', width = .25) +
  geom_boxplot(width = .25) +
  theme_bw() +
  theme(
    axis.text.x = element_blank(),
    axis.ticks.x = element_blank(),
    axis.title.x = element_blank(),
    panel.grid.minor = element_blank(),
    panel.grid.major.x = element_blank()
  ) +
  scale_y_continuous(breaks = seq(20, 80, 10)) +
  scale_x_continuous(limits = c(0, 2))

  labs(y = 'Age')
$y
[1] "Age"

attr(,"class")
[1] "labels"
ggplot(df, aes(y = age, x = infection, fill = infection)) +
  stat_boxplot(geom = 'errorbar', width = .25) +
  geom_boxplot(width = .25) +
  geom_jitter(height = 0, width = .1)

ggplot(df, aes(y = infection, x = age, fill = infection)) +
  stat_boxplot(geom = 'errorbar', width = .25) +
  geom_boxplot(width = .25) +
  geom_jitter(height = .1, width = 0)

ggplot(data = df, mapping = aes(x = infection, y = age, fill = infection)) +
  geom_boxplot() +
  geom_dotplot(method = 'histodot', 
               binwidth = 1,
               binaxis = 'y',
               stackdir = 'center') +
  stat_summary(fun = median, geom = 'crossbar')

ggplot(df, aes(y = age, x = infection, fill = infection)) +
  stat_boxplot(geom = 'errorbar', width = .25) +
  geom_boxplot(width = .25, outlier.shape = NA) +
  geom_jitter(height = 0, width = .1, aes(color = sex)) +
  theme_light(base_size = 20) +
  theme(
    legend.position = 'bottom'
  ) + 
  labs(x = 'Infection Status', y = 'Age', color = 'Sex', fill = 'Infection') + 
  scale_fill_brewer(palette = 'Set3') + 
  scale_color_brewer(palette = 'Set1')