I came back to R to make some graph for publication figure. However, I'm quite rusty and still in the learning process and I'm struggling on bar/col-based graph generation.
Context: Basically we have result of in vitro assays made with different type of cells from drifferent patients. The variable are the following:
I would like to generate a graph were barplot represent the value of in vitro assay according to the dilution for one cell type/condition (1:3, 1:10, 1:30 for one condition, then 1:3, 1:10, 1:30 for a second, etc). A bit like this:
I've run the following code with a dummy data frame
df <- data.frame(
Patient = rep(c("Ptn1", "Ptn2", "Ptn3"), each = 8),
Cells = rep(rep(c("T1", "T2"), each = 3), 4),
Ratio = rep(c("1:3", "1:10", "1:30"), 8),
Value = c(0.2, 0.5, 0.9, 0.7, 0.8, 0.9, 0.1, 0.4, 0.8, 0.6, 0.8, 0.9, 0.15, 0.45, 0.85, 0.55, 0.85, 0.95, 0.1, 0.3, 0.8, 0.7, 0.8, 0.9))
df$variable <- interaction(df$Cells, df$Ratio)
df$variable <- factor(df$variable, levels = c("T1.1:30", "T1.1:10", "T1.1:3", "T2.1:30", "T2.1:10", "T2.1:3"))
levels(df$variable)
ggplot(df, aes(x = variable, y = Value)) + geom_col()
Which gave me this graph:
What is weird is that none of my value in df are above 1 (0.1 to 0.9) but y scale is giving value going up to 3 or 4. I have the same issue with the real dataset.
Also, what would be the elegant way to "gather" bar according to one condition/cell type and puting a small gap between cell type/condition?
Thanks for your help
geom_col()
doesn't compute any summary information, but instead leaves y
as-is and then stacks the result. I think you might be looking for geom_bar()
with stat = "summary"
.
Moreover, reconsider dynamite plots.
library(ggplot2)
df <- data.frame(
Patient = rep(c("Ptn1", "Ptn2", "Ptn3"), each = 8),
Cells = rep(rep(c("T1", "T2"), each = 3), 4),
Ratio = rep(c("1:3", "1:10", "1:30"), 8),
Value = c(0.2, 0.5, 0.9, 0.7, 0.8, 0.9, 0.1, 0.4, 0.8, 0.6, 0.8, 0.9, 0.15, 0.45, 0.85, 0.55, 0.85, 0.95, 0.1, 0.3, 0.8, 0.7, 0.8, 0.9))
df$variable <- interaction(df$Cells, df$Ratio)
df$variable <- factor(df$variable, levels = c("T1.1:30", "T1.1:10", "T1.1:3", "T2.1:30", "T2.1:10", "T2.1:3"))
ggplot(df, aes(x = variable, y = Value)) +
geom_errorbar(
stat = "summary",
fun.data = ~ mean_sdl(.x, mult = 1),
width = 0.2
) +
geom_bar(
stat = "summary", fun = mean
)
Created on 2022-08-04 by the reprex package (v2.0.1)