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
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")