Add numbers to each bar: fread(newparameterspath) %>% filter(grepl('dgold', model)) %>%…
- User
Add numbers to each bar: fread(new_parameters_path) %>% filter(grepl('dg_old', model)) %>% select(gene_id, gene_name, model) %>% distinct() %>% left_join(fread(model_classifications_path)) %>% rename(new_model = model) %>% left_join(fread(old_parameters_path)) %>% rename(old_model = model) %>% ggplot(aes(old_model, fill = classification)) + geom_bar(position = 'dodge') + theme_classic(base_size = 20) + xlab("Old degradation model") + ylab("Number of genes") + scale_fill_manual( values = c("MZ" = '#1b9e77', "M" = '#d95f02', "Z" = '#7570b3') ) +
- Assistant
You can add numbers to each bar (i.e., counts) using geom_text() with the same grouping as your bars. Here’s the modified version of your code: rCopy codefread(new_parameters_path) %>% filter(grepl('dg_old', model)) %>% select(gene_id, gene_name, model) %>% distinct() %>% left_join(fread(model_classifications_path)) %>% rename(new_model = model) %>% left_join(fread(old_parameters_path)) %>% rename(old_model = model) %>% ggplot(aes(old_model, fill = classification)) + geom_bar(position = position_dodge()) + geom_text( stat = 'count', aes(label = ..count..), position = position_dodge(width = 0.9), vjust = -0.5, size = 5 ) + theme_classic(base_size = 20) + xlab("Old degradation model") + ylab("Number of genes") + scale_fill_manual( values = c("MZ" = '#1b9e77', "M" = '#d95f02', "Z" = '#7570b3') )
Notes:
stat = "count" makes geom_text() use the same counting statistic as geom_bar().
position_dodge(width = 0.9) ensures the labels align with the bars.
vjust = -0.5 moves the labels slightly above the bars.
You can adjust size or vjust to fine-tune appearance.
Would you like the numbers inside the bars instead (for a cleaner look)?