u/ashwithasmile

Help with R code!!!

https://preview.redd.it/3hg1pzxci52h1.png?width=1094&format=png&auto=webp&s=bdec36e242db6aa1232374fdd0146a813541c0e2

Hi all,

I am trying to make a histogram of some data I collected. I am trying to stratify my "baseline" data by biological replicate and all other data (a separate category called ExM) by gel. I want it to look something like what I've drawn below. This is the code I am currently working with and the current output. Any recommendations will be greatly appreciated!

# mean + SD per gel × tissue_type

gel_stats <- DAPI_Quant %>%

group_by(gel, tissue_type) %>%

summarise(

mean_avg = mean(ratio, na.rm = TRUE),

sd_avg = sd(ratio, na.rm = TRUE),

.groups = "drop"

)

# Convert gel to character

gel_stats$gel <- as.character(gel_stats$gel)

# Create color palette

unique_gels <- unique(gel_stats$gel)

# ExM palette (Dark2)

exm_palette <- scales::brewer_pal(type = "qual", palette = "Dark2")(length(unique_gels))

gel_colors <- setNames(exm_palette, unique_gels)

# Baseline override (curve color)

gel_colors["baseline"] <- "grey30"

# plot styling + axes

p <- ggplot() +

theme_classic(base_size = 10) +

theme(

legend.position = "right",

axis.title = element_text(size = 10),

axis.text = element_text(size = 10),

plot.margin = margin(10, 10, 10, 10)

) +

xlim(0.75,1.35) + ylim(0, 15)+

labs(

x = "XY Ratio",

y = "Density"

)

# histogram per gel

for (g in unique_gels) {

df_gel <- DAPI_Quant %>% filter(gel == g)

# Baseline bars grey, ExM bars keep their original color

hist_fill <- ifelse(g == "baseline", "grey30", gel_colors[g])

p <- p +

geom_histogram(

data = df_gel,

aes(x = ratio, y = after_stat(density)),

bins = 150,

fill = hist_fill,

alpha = 0.25,

color = NA

)

}

for (i in 1:nrow(gel_stats)) {

row <- gel_stats[i, ]

g <- row$gel

# Baseline curve grey, ExM curves keep their colors

curve_col <- ifelse(row$tissue_type == "baseline", "grey30", gel_colors[g])

p <- p + stat_function(

fun = dnorm,

args = list(mean = row$mean_avg, sd = row$sd_avg),

color = curve_col,

linewidth = ifelse(row$tissue_type == "baseline", 1.4, 1.1),

lineend = "round",

inherit.aes = FALSE

)

}

p

https://preview.redd.it/nhd1e4poh52h1.png?width=1132&format=png&auto=webp&s=2c9832379f59bc7543282334da02870e7623a6ad

reddit.com
u/ashwithasmile — 3 days ago