r - how to make lines between stack bar charts? - Stack Overflow

admin2025-04-22  4

I have trouble making line between two paired stacked boxplot. Im comparing pre ->post cellular composition change, though hard to draw line between the plots. shown below is my current script. I want to change my plot (plot1) into the plot shown below(plot2) Many thanks guys

data <- data.frame(
  Group = rep(c("Responder", "Non-responder"), each = 16), # 16 x 2 = 32
  Time = rep(c("Pre", "Post"), times = 16),               # 16 x 2 = 32
  CellType = rep(c("B", "Mono", "DC", "CD4_T", "CD8_T", "NK", "Other", "Platelet"), each = 2, times = 2), # 길이 32
  Proportion = runif(32, 0.01, 0.2)                       
)

data <- data %>%
  group_by(Group, Time) %>%
  mutate(Proportion = Proportion / sum(Proportion)) %>% 
  ungroup()

# Stacked Bar Plot 
ggplot(data, aes(x = Time, y = Proportion, fill = CellType)) +
  geom_bar(stat = "identity", position = "stack") + # Stacked bar plot
  facet_wrap(~ Group, ncol = 2) +                 
  scale_fill_brewer(palette = "Paired") +         
  theme_minimal() +                                
  labs(
    title = "Cell Type Proportions Before and After Treatment",
    x = "Time",
    y = "Cell Proportions",
    fill = "Cell Type"
  )

Plot1, my boxplot Plot2, I want to make my plots like this

Ive searched through google and stack overflow and found some examples, though im still novice,,, hard to implement those codes into mine, sorry guys.. help me pls

I have trouble making line between two paired stacked boxplot. Im comparing pre ->post cellular composition change, though hard to draw line between the plots. shown below is my current script. I want to change my plot (plot1) into the plot shown below(plot2) Many thanks guys

data <- data.frame(
  Group = rep(c("Responder", "Non-responder"), each = 16), # 16 x 2 = 32
  Time = rep(c("Pre", "Post"), times = 16),               # 16 x 2 = 32
  CellType = rep(c("B", "Mono", "DC", "CD4_T", "CD8_T", "NK", "Other", "Platelet"), each = 2, times = 2), # 길이 32
  Proportion = runif(32, 0.01, 0.2)                       
)

data <- data %>%
  group_by(Group, Time) %>%
  mutate(Proportion = Proportion / sum(Proportion)) %>% 
  ungroup()

# Stacked Bar Plot 
ggplot(data, aes(x = Time, y = Proportion, fill = CellType)) +
  geom_bar(stat = "identity", position = "stack") + # Stacked bar plot
  facet_wrap(~ Group, ncol = 2) +                 
  scale_fill_brewer(palette = "Paired") +         
  theme_minimal() +                                
  labs(
    title = "Cell Type Proportions Before and After Treatment",
    x = "Time",
    y = "Cell Proportions",
    fill = "Cell Type"
  )

Plot1, my boxplot Plot2, I want to make my plots like this

Ive searched through google and stack overflow and found some examples, though im still novice,,, hard to implement those codes into mine, sorry guys.. help me pls

Share Improve this question asked Jan 21 at 14:38 juyong seongjuyong seong 11 1
  • 2 Your wanted output is this plot. Can't you adapt it to your problem? – Rui Barradas Commented Jan 21 at 15:14
Add a comment  | 

1 Answer 1

Reset to default 0

Here's how I would solve it: Make Time a factor so you can use it as a numeric to position the lines on x. Then yuse a conditional to decide if they should start to the right or the left of the bar.

library(tidyverse)

data <- data.frame(
  Group = rep(c("Responder", "Non-responder"), each = 16), # 16 x 2 = 32
  Time = rep(c("Pre", "Post"), times = 16),               # 16 x 2 = 32
  CellType = rep(c("B", "Mono", "DC", "CD4_T", "CD8_T", "NK", "Other", "Platelet"), each = 2, times = 2), # 길이 32
  Proportion = runif(32, 0.01, 0.2)                       
)

data %>%
  group_by(Group, Time) %>%
  mutate(
    Proportion = Proportion / sum(Proportion), 
    Time = factor(Time)
    ) %>%
  ungroup() %>% 
  ggplot(
    aes(x = Time, y = Proportion, fill = CellType)) +
  geom_bar(stat = "identity", position = "stack", width = .5) + # Stacked bar plot
  facet_wrap(~ Group, ncol = 2) +                 
  scale_fill_brewer(palette = "Paired") +         
  theme_minimal() +                                
  labs(
    title = "Cell Type Proportions Before and After Treatment",
    x = "Time",
    y = "Cell Proportions",
    fill = "Cell Type"
  ) + 
  geom_line(
    aes(
      x = ifelse (Time == "Post", as.numeric(Time)+0.25, as.numeric(Time)-.25), 
      y = Proportion, 
      group = CellType), 
  position = position_stack(), 
  linetype = "dotted")

转载请注明原文地址:http://anycun.com/QandA/1745303981a90619.html