简体   繁体   中英

How to connect paired dots in dodged dotplots with ggplot2 in R?

I have several variables measured twice and I want to display them in a chart showing boxplots as well as individual values (using geom_dotplot ) where the values from the two measurements in the same variable are connected by a customisable line.

I have found a solution using jittered geom_point (basically generating a new data) but if at all possible, I would like to have a dotplot.

Simplified code to create the chart for 5 subjects and 2 variables

library(ggplot2)


dat <- data.frame(subject     = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5), 
                  measurement = c("M1", "M1", "M1", "M1", "M1", "M2", "M2", "M2", "M2", "M2",
                                  "M1", "M1", "M1", "M1", "M1", "M2", "M2", "M2", "M2", "M2"), 
                  variable    = c("Var1", "Var1", "Var1", "Var1", "Var1", "Var1", "Var1", "Var1", "Var1", "Var1",
                                  "Var2", "Var2", "Var2", "Var2", "Var2", "Var2", "Var2", "Var2", "Var2", "Var2"), 
                  value       = c(20, 15, 21, 23, 15, 22, 21, 31, 24, 17, 26, 10, 23, 22, 19, 29, 15, 25, 29, 19))


ggplot(dat, aes(variable, value, colour = measurement)) +
  geom_boxplot(outlier.shape = NA) + 
  geom_dotplot(aes(fill = measurement),
               binaxis = "y", binwidth = 0.5, stackdir = "center", 
               position = position_dodge(0.75))

The current chart where I would like to have lines connecting paired red and cyan dots between the two measurements within a variable, as shown for Var1 where I drawn them by hand: 在此处输入图像描述

Thanks, PB

You can achieve a similar output by using facet_wrap and specifying aes(group = subject) , so each gray line is an individual subject. I've provided two options, one split by variable and the other split by measurement. The second plot is probably closer to your original idea.

library(tidyverse)

dat <- data.frame(subject     = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5), 
                  measurement = c("M1", "M1", "M1", "M1", "M1", "M2", "M2", "M2", "M2", "M2",
                                  "M1", "M1", "M1", "M1", "M1", "M2", "M2", "M2", "M2", "M2"), 
                  variable    = c("Var1", "Var1", "Var1", "Var1", "Var1", "Var1", "Var1", "Var1", "Var1", "Var1",
                                  "Var2", "Var2", "Var2", "Var2", "Var2", "Var2", "Var2", "Var2", "Var2", "Var2"), 
                  value       = c(20, 15, 21, 23, 15, 22, 21, 31, 24, 17, 26, 10, 23, 22, 19, 29, 15, 25, 29, 19))

# split by measurement
dat %>% 
  mutate(new = factor(interaction(measurement, variable))) %>%
  ggplot(aes(x = new, y = value, color = measurement)) + 
  geom_boxplot(outlier.shape = NA) + 
  geom_dotplot(aes(fill = measurement),
               binaxis = "y", binwidth = 0.5, stackdir = "center", 
               position = position_dodge(0.75)) +
  geom_line(aes(group = subject), color = "gray") +
  facet_wrap(~measurement, scales = "free") +
  scale_x_discrete(labels = c("Var1", "Var2")) +
  xlab("")

在此处输入图像描述

# split by variable
dat %>% 
  mutate(new = factor(interaction(variable, measurement))) %>%
  ggplot(aes(x = new, y = value, color = variable)) + 
  geom_boxplot(outlier.shape = NA) + 
  geom_dotplot(aes(fill = variable),
               binaxis = "y", binwidth = 0.5, stackdir = "center", 
               position = position_dodge(0.75)) +
  geom_line(aes(group = subject), color = "gray") +
  facet_wrap(~variable, scales = "free") +
  scale_x_discrete(labels = c("M1", "M2")) + 
  xlab("")

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM