yenats
yenats

Reputation: 551

R: ggplot2: how to separate labels in stat_summary

I try to plot labels above bars with the stat_summary function and a custom function that I wrote. There are three bars and each should be labeled with the letters a:c, respectively. However, instead of putting one label per bar, all three labels are placed on top of each other:

codes <- c ("a", "b", "c")

simple_y <- function(x) {
        return (data.frame (y = mean (x) + 1, label = codes))
        }


    ggplot (iris, mapping = aes (x = Species, y = Sepal.Length)) +
        geom_bar (stat = "summary", fun.y = "mean", fill = "blue", width = 0.7, colour = "black", size = 0.7) +
        stat_summary (fun.data = simple_y, geom = "text", size = 10)

enter image description here

I do understand why this is not working: each time the simply_y-function is recycled, it sees the whole codes - vector. However, I have no clue how to tell R to separate the three labels. Is it possible to tell R to subsequently use the n_th element of an input-vector when recycling a function?

Does anybody have a good hint?

Upvotes: 0

Views: 545

Answers (1)

cardinal40
cardinal40

Reputation: 1273

I would consider doing something like this:

labels <- 
  tibble(
    Species = factor(c("setosa", "versicolor", "virginica")),
    codes = c("a", "b", "c")
  )

iris %>% 
  group_by(Species) %>% 
  summarize(Mean = mean(Sepal.Length)) %>% 
  ungroup() %>% 
  left_join(labels, by = "Species") %>% 
  ggplot(aes(x = Species, y = Mean)) +
  geom_col(fill = "blue", width = 0.7, color = "black", size = 0.7) +
  geom_text(aes(y = Mean + 0.3, label = codes), size = 6, show.legend = FALSE)

First, you can generate the data frame with means separately, avoiding the need for geom_bar and stat_summary. Then after joining the manual labels/codes to that summarized data frame, it's pretty straightforward to add them with geom_text.

enter image description here

Upvotes: 2

Related Questions