Skip to content

Instantly share code, notes, and snippets.

@ctesta01
Last active February 7, 2022 15:43
Show Gist options
  • Select an option

  • Save ctesta01/d01cf52de2cee3dc80e856743cd6d856 to your computer and use it in GitHub Desktop.

Select an option

Save ctesta01/d01cf52de2cee3dc80e856743cd6d856 to your computer and use it in GitHub Desktop.
Putting dotplots for two distinct groups on either side of the x-axis
library(ggplot2)
library(dplyr)
library(magrittr)
# generate fake data
total_N_obs <- 1000
example_data <-
data.frame(
year = sample(2000:2020, replace = TRUE, size = total_N_obs),
type = sample(c('a', 'b'), replace = TRUE, size = total_N_obs)
)
# skew the data towards 2020
example_data %<>% rowwise() %>% mutate(
year = year + sample(0:(2020-year), size = 1))
# make plot
ggplot() +
geom_dotplot(
data = example_data %>% filter(type == 'a'),
mapping = aes(x = year, fill = type),
stackdir = 'up',
method = 'histodot'
) +
geom_dotplot(
data = example_data %>% filter(type == 'b'),
mapping = aes(x = year, fill = type),
stackdir = 'down',
method = 'histodot'
) +
ggtitle("Example plot with dotplots on either side of the x-axis")
@ctesta01
Copy link
Author

ctesta01 commented Feb 4, 2022

example plot produced, dotplots on either side of the x-axis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment