Hh Xu
Hh Xu

Reputation: 43

add legend labels to bar graph of ggplot

I would like to add cluster names to the stacked barplot in ggplot.

enter image description here

As you can see, some clusters had very small fraction, and there are many clusters in each bar. This makes it challenging to see which clusters are which, especially between similar colors. Ideally I would like to selectively label some big fractions (or cluster) with its name, say when the percentage is > 5%, show its cluster name.

For example, in the sample 'naive CD8 T', I want to label the clusters 4 and 5, like this:

enter image description here

cluster.count %>% 
  ggplot(aes(x=Cell_subtype,y=count1, fill= seurat_clusters)) +
  geom_bar(stat="identity", position = 'fill')

Upvotes: 3

Views: 1184

Answers (1)

stefan
stefan

Reputation: 125418

This could be achieved like so:

  1. Instead of making use of position="fill" I compute the percentages manually via group_by + mutate
  2. The labels could then be easily added via a geom_text where you can use an ifelse to display only labels with a desired minimum frequency or proportion.

Making use of some random example data try this:

library(ggplot2)
library(dplyr)

set.seed(42)
cluster.count <- data.frame(
  Cell_subtype = sample(LETTERS[1:4], 60, replace = TRUE),
  seurat_clusters = sample(0:16, 60, replace = TRUE)
)
cluster.count <- count(cluster.count, Cell_subtype, seurat_clusters, name = "count1")
cluster.count <- mutate(cluster.count, seurat_clusters = factor(seurat_clusters))

cluster.count %>% 
  group_by(Cell_subtype) %>% 
  mutate(pct = count1 / sum(count1)) %>% 
  ggplot(aes(x=Cell_subtype,y=pct, fill= seurat_clusters)) +
  geom_col() +
  geom_text(aes(label = ifelse(pct > .1, as.character(seurat_clusters), "")), position = position_stack(vjust = .5))

Upvotes: 3

Related Questions