thisisrg thisisrg - 3 months ago 14
R Question

Adding text in stacked bar where levels are ordered using stat=summary

Here is the data:

df_test<-structure(list(MIRNA = c("let-7c", "let-7c", "let-7c", "let-7c",
"let-7c", "let-7c", "let-7c", "mir-125b-2", "mir-125b-2", "mir-125b-2",
"mir-125b-2", "mir-125b-2", "mir-125b-2", "mir-125b-2", "mir-155",
"mir-155", "mir-155", "mir-155", "mir-155", "mir-155", "mir-155",
"mir-4760", "mir-4760", "mir-4760", "mir-4760", "mir-4760", "mir-4760",
"mir-4760", "mir-548x", "mir-548x", "mir-548x", "mir-548x", "mir-548x",
"mir-548x", "mir-6501", "mir-6501", "mir-6501", "mir-6501", "mir-6501",
"mir-6501", "mir-6501", "mir-6508", "mir-6508", "mir-6508", "mir-6508",
"mir-6508", "mir-6508", "mir-6508", "mir-6814", "mir-6814", "mir-6814",
"mir-6814", "mir-6814", "mir-6814", "mir-6815", "mir-6815", "mir-6815",
"mir-6815", "mir-6815", "mir-6815", "mir-99a", "mir-99a", "mir-99a",
"mir-99a", "mir-99a", "mir-99a", "mir-99a"), MIRNA_Feature = structure(c(6L,
5L, 3L, 2L, 7L, 1L, 4L, 6L, 5L, 3L, 2L, 7L, 1L, 4L, 6L, 5L, 3L,
2L, 7L, 1L, 4L, 6L, 5L, 3L, 2L, 7L, 1L, 4L, 6L, 5L, 3L, 2L, 7L,
1L, 6L, 5L, 3L, 2L, 7L, 1L, 4L, 6L, 5L, 3L, 2L, 7L, 1L, 4L, 6L,
5L, 3L, 2L, 1L, 4L, 6L, 5L, 3L, 2L, 1L, 4L, 6L, 5L, 3L, 2L, 7L,
1L, 4L), .Label = c("Precursor_5PrimeEnd", "5p_Seed", "5p_Mature",
"Precursor_Loop", "3p_Seed", "3p_Mature", "Precursor_3PrimeEnd"
), class = "factor"), domain_length = c(13L, 9L, 13L, 9L, 7L,
10L, 23L, 13L, 9L, 13L, 9L, 14L, 16L, 15L, 13L, 9L, 14L, 9L,
1L, 3L, 16L, 13L, 9L, 13L, 9L, 7L, 9L, 20L, 14L, 9L, 11L, 9L,
10L, 45L, 14L, 9L, 13L, 9L, 1L, 2L, 19L, 13L, 9L, 12L, 9L, 2L,
4L, 11L, 13L, 9L, 13L, 9L, 5L, 21L, 12L, 9L, 14L, 9L, 5L, 12L,
13L, 9L, 13L, 9L, 10L, 12L, 15L), order = c(6L, 5L, 3L, 2L, 7L,
1L, 4L, 6L, 5L, 3L, 2L, 7L, 1L, 4L, 6L, 5L, 3L, 2L, 7L, 1L, 4L,
6L, 5L, 3L, 2L, 7L, 1L, 4L, 6L, 5L, 3L, 2L, 7L, 1L, 6L, 5L, 3L,
2L, 7L, 1L, 4L, 6L, 5L, 3L, 2L, 7L, 1L, 4L, 6L, 5L, 3L, 2L, 1L,
4L, 6L, 5L, 3L, 2L, 1L, 4L, 6L, 5L, 3L, 2L, 7L, 1L, 4L), expr = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2.6,
1, 1, 1, 1, 1, 1, 1, 1, 1, 3.4, 1, 1, 3.6, 2.6, 1, 1, 1, 1, 1,
2.4, 1, 1, 6, 3.4, 1, 1, 1, 1, 1, 1, 2.4, 1, 1, 1, 1, 2.8, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1.6)), .Names = c("MIRNA", "MIRNA_Feature",
"domain_length", "order", "expr"), row.names = c("29", "30",
"31", "32", "33", "34", "35", "334", "335", "336", "337", "338",
"339", "340", "695", "696", "697", "698", "699", "700", "701",
"3084", "3085", "3086", "3087", "3088", "3089", "3090", "4111",
"4112", "4113", "4114", "4115", "4116", "4433", "4434", "4435",
"4436", "4437", "4438", "4439", "4481", "4482", "4483", "4484",
"4485", "4486", "4487", "5260", "5261", "5262", "5263", "5264",
"5265", "5266", "5267", "5268", "5269", "5270", "5271", "6098",
"6099", "6100", "6101", "6102", "6103", "6104"), class = "data.frame")


I order them according to the levels in the MIRNA_Feature column like so:

df_test$MIRNA_Feature<-factor(as.factor(df_test$MIRNA_Feature),levels=c("Precursor_5PrimeEnd","5p_Seed","5p_Mature","Precursor_Loop","3p_Seed","3p_Mature","Precursor_3PrimeEnd"))


Then plot the stacked bargraph with labels and get this plot:

ggplot(df_test,aes(x=MIRNA,y=domain_length,fill = MIRNA_Feature))+geom_bar(stat="identity")+geom_label(aes(label=expr),position="stack")+coord_flip()


enter image description here

The problem is the order that I specified got lost.I could obtain the desired order using stat=summary like so but then the order of the labels are all off:

ggplot(df_test,aes(x=MIRNA,y=domain_length,fill = MIRNA_Feature))+geom_bar(stat="summary",fun.y=sum)+geom_label(aes(label=expr),position="stack")+coord_flip()


The plot looks like this:
enter image description here

Looks like the ordering of the labels are done separately (alphabetically) and the stacks are done according to the specified order when using stat="summary". Any help in resolving this is much appreciated.

Answer

In ggplot version 2.1, stat_summary preserves the order in the data frame, so you need to sort the values beforehand. For example:

ggplot(df_test[order(df_test$MIRNA, df_test$MIRNA_Feature),],
       aes(x=MIRNA,y=domain_length,fill = MIRNA_Feature)) + 
  geom_bar(stat="summary", fun.y=sum) +
  geom_label(aes(label=expr), position="stack") +
  coord_flip()

enter image description here

Centering the labels within each bar would make the plot a bit less confusing. You can do this by creating a new column (which I've called y.pos) to set the label position. I've used the chaining operator (%>%) from the dplyr package to streamline this:

library(dplyr)

df_test %>% 
  group_by(MIRNA) %>% 
  arrange(MIRNA, MIRNA_Feature) %>%
  mutate(y.pos = cumsum(domain_length) - 0.5*domain_length) %>%
ggplot(aes(x=MIRNA, fill = MIRNA_Feature)) + 
  geom_bar(stat="summary", aes(y=domain_length), fun.y=sum) +
  geom_label(aes(label=expr, y=y.pos), size=2.7, 
             label.padding=unit(0.15, "lines"), show.legend=FALSE) +
  coord_flip()

enter image description here

Comments