Search code examples
rggplot2textannotationsgeom-text

Add custom text by onto each bars


Can someone please help me put text onto each bar. I am using geomtext function but not sure how it works to put text on each bar Here is code:

df <- read.csv("F:/open_end_frequency_response.csv")
colnames(df)[1] = "modality"
colnames(df)[2] = "First_response"
colnames(df)[3] = "Second_response"
colnames(df)[4] = "Third_response"
colnames(df)[5] = "Fourth_response"
colnames(df)[6] = "Fifth_response"
dput(df)
colnames(df) <- c("modality","First_response","Second_response","Third_response",
                  "Fourth_response", "Fifth_response", "Frequency")

df2 <- select(df, -Frequency)
dat <- melt(df2)

#Using modality as id variables
p1 <- ggplot(dat, aes(modality, value, fill=interaction(variable))) +
  geom_bar(stat='identity', position='dodge') +
  ggtitle("A: General OEQ") + 
  theme_bw() + theme(axis.text.x = element_text(angle=90, hjust=1, size = 10, face = "bold")) +
  scale_fill_brewer('Open-end', palette='RdPu') +  theme(legend.position = c(.51, .80)) +
  theme(legend.title=element_blank()) +
  xlab("") + ylab("Frequency") + theme(panel.border = element_blank()) + 
  theme(axis.line = element_line(colour = "black")) + 
  theme(legend.key.size = unit(1.0, "cm"), legend.key.width = unit(0.5,"cm"),
        legend.key.height = unit(0.5,"cm")) + 
    theme(legend.text = element_text(size = 7)) +
  geom_text(aes(label = "a"), position = position_dodge(0.9), size = 2,
            vjust = -0.25, fontface="bold")

Please find below a piece of my data, I hope this will help you play with the data. Also I am not sure how to share data on stackoverflow, please if someone can share me the link:

squads <- tibble::tribble(
                   ~test, ~first_res, ~second_res, ~third_res, ~fourth_res, ~fifth_res,
            "Appearance",       201L,         72L,        54L,         46L,        39L,
                 "Aroma",         8L,         17L,        17L,         24L,        13L,
                "Flavor",       107L,        148L,       177L,        168L,       122L,
               "Texture",       151L,        225L,       220L,        198L,       150L,
              "Abstract",       282L,        260L,       360L,        356L,       402L
            )
head(squads)
#> # A tibble: 5 x 6
#>   test       first_res second_res third_res fourth_res fifth_res
#>   <chr>          <int>      <int>     <int>      <int>     <int>
#> 1 Appearance       201         72        54         46        39
#> 2 Aroma              8         17        17         24        13
#> 3 Flavor           107        148       177        168       122
#> 4 Texture          151        225       220        198       150
#> 5 Abstract         282        260       360        356       402

Created on 2021-09-01 by the reprex package (v2.0.1)


Solution

  • for example, as you need 25 texts, let x <- c(1:25)

    add geom_text(aes(label = x), position = position_dodge(width = 0.9), vjust = -0.25) like

    ggplot(dat, aes(modality, value, fill=interaction(variable))) +
      geom_bar(stat='identity', position='dodge') +
      ggtitle("A: General OEQ") + 
      theme_bw() + theme(axis.text.x = element_text(angle=90, hjust=1, size = 10, face = "bold")) +
      scale_fill_brewer('Open-end', palette='RdPu') +  theme(legend.position = c(.51, .80)) +
      theme(legend.title=element_blank()) +
      xlab("") + ylab("Frequency") + theme(panel.border = element_blank()) + 
      theme(axis.line = element_line(colour = "black")) + 
      theme(legend.key.size = unit(1.0, "cm"), legend.key.width = unit(0.5,"cm"),
            legend.key.height = unit(0.5,"cm")) + 
      theme(legend.text = element_text(size = 7)) +
      geom_text(aes(label = x), position = position_dodge(width = 0.9), vjust = -0.25)
    

    then result is like

    1~25

    plot_grid(p1, p2, labels = c("p1", "p2"))
    

    add

    Sequence replaced

    abcd

    Final code Edit (I guess)

    In dat <- melt(df2), add some code as dat <- melt(df2) %>% arrange(modality). Then, for example x <- c(1:25), dat$text <- x. Now, code to plot is now

    p1 <- ggplot(dat, aes(modality, value, fill=interaction(variable))) +
      geom_bar(stat='identity', position='dodge') +
      ggtitle("A: General OEQ") + 
      theme_bw() + theme(axis.text.x = element_text(angle=90, hjust=1, size = 10, face = "bold")) +
      scale_fill_brewer('Open-end', palette='RdPu') +  theme(legend.position = c(.51, .80)) +
      theme(legend.title=element_blank()) +
      xlab("") + ylab("Frequency") + theme(panel.border = element_blank()) + 
      theme(axis.line = element_line(colour = "black")) + 
      theme(legend.key.size = unit(1.0, "cm"), legend.key.width = unit(0.5,"cm"),
            legend.key.height = unit(0.5,"cm")) + 
      theme(legend.text = element_text(size = 7)) +
      geom_text(aes(label = text, group = ), position = position_dodge(width = 0.9), vjust = -0.25)
    

    sequence right