Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a line plot with attractive raw data visualisation using R?

I wish to draw a line plot with error bar across two groups (like here), but also with a violin plot or raincloud plot or some other form of visualising the raw data too (since summary statistics like SD, SE, CI etc. may not paint the whole picture).

I am attaching a reproducible example for a line plot similar to the attached link, but I am not sure how to add the raw data visualisation in it

library(dplyr)
library(ggplot2)

d1 <-  data.frame(time_serie = as.factor(rep(rep(1:3, each = 6), 3)),
                  treatment = as.factor(rep(c("HIGH", "MEDIUM", "LOW"), each = 18)),
                  value = runif(54, 1, 10))

d2 <- d1 %>%
  dplyr::group_by(time_serie,treatment) %>%
  dplyr::summarise(mean_value = mean(value), 
                   sd_value = sd(value)) %>%
  as.data.frame()

ggplot(aes(x = time_serie, y = mean_value, color = treatment, group = treatment), data=d2) + 
  geom_errorbar(aes(ymin = mean_value - sd_value, ymax = mean_value + sd_value), width = .2, position = position_dodge(0.3), size =1) +
  geom_point(aes(), position = position_dodge(0.3), size = 3) + 
  geom_line(aes(color = treatment), position=position_dodge(0.3), size =1)

enter image description here

like image 441
Muhammad Aaqib Shamim Avatar asked Jan 25 '26 21:01

Muhammad Aaqib Shamim


1 Answers

library(dplyr)
library(ggplot2)

set.seed(42)

d1 <-  data.frame(time_serie = as.factor(rep(rep(1:3, each = 6), 3)),
                  treatment = as.factor(rep(c("HIGH", "MEDIUM", "LOW"), 
                                            each = 18)),
                  value = runif(54, 1, 10))

d2 <- d1 %>%
       summarize(mean_value = mean(value), 
                 sd_value = sd(value),
                 .by = c(time_serie, treatment))

ggplot(data = d2,
       aes(x = time_serie, y = mean_value, 
           color = treatment, group = treatment)) + 
  geom_violin(data = d1,
              aes(x = time_serie, y = value, 
                  color = treatment, fill = treatment,
                  group = interaction(treatment, time_serie)), 
              alpha = 0.1, position = position_dodge(0.3), 
              inherit.aes = FALSE) +
  geom_errorbar(aes(ymin = mean_value - sd_value, 
                    ymax = mean_value + sd_value), 
                width = .2, position = position_dodge(0.3), linewidth = 1) +
  geom_point(position = position_dodge(0.3), size = 3) + 
  geom_line(aes(color = treatment), 
            position = position_dodge(0.3), linewidth = 1) 

Created on 2025-01-03 with reprex v2.0.2

like image 132
M-- Avatar answered Jan 28 '26 12:01

M--