Search code examples
rgeom-text

How to add text with geom_text over geom_bars?


I would like to add names of the questions over the bars that represent the anserws

enter image description here

Problem is that the text comes twice on to the bars. The blue and orange (fill) represent first and second evaluation, but they represent the same question. So what I would like to do is that the text (question) would only be printed once since the same question is asked in both evaluations and that the position would adjustable. How would I achieve this with the geom_text? Another way I thought was that the scale_x_discrete would be positioned differently, but I figured it can only be positioned to top, bottom, left or right.

plot_data <- structure(list(kunta = c("Kau", "Kau", "Kau", "Kau", "Kau", "Kau", 
"Kau", "Kau"), Arviointi = c("Ensimmäinen arviointi", "Ensimmäinen arviointi", 
"Ensimmäinen arviointi", "Ensimmäinen arviointi", "Toinen arviointi", 
"Toinen arviointi", "Toinen arviointi", "Toinen arviointi"), 
    Mittari = c("onn", "pkm", "tyy", "yk", "onn", "pkm", "tyy", 
    "yk"), value = c(0.935483870967742, 0.90625, 0.75, 0.161290322580645, 
    0.972972972972973, 0.794871794871795, 0.769230769230769, 
    0.027027027027027), Kansallinen = c(0.909396809571286, 0.754092238750233, 
    0.697411911443717, 0.116863721219664, 0.919229103508012, 
    0.819420873992363, 0.727938834023574, 0.0934428000432105)), row.names = c(NA, 
-8L), class = c("tbl_df", "tbl", "data.frame"))


ggplot(plot_data, aes(x = Mittari, y = value, fill = Arviointi)) +
   geom_bar(stat = "identity", position = position_dodge2(reverse = TRUE), width = 0.45, alpha = 1) +
   geom_errorbar(data = plot_data, aes(x = Mittari, ymin = Kansallinen, ymax = Kansallinen, group = Arviointi,           
   width  = 0.45), position = position_dodge2(reverse = TRUE)) +    
   geom_text(aes(group = Mittari, label = ("text here")), hjust=-0.5, colour = "black", position = "dodge") +
   scale_fill_manual(values = c("steelblue", "darkorange")) +
   scale_color_manual(values = c("steelblue", "darkorange")) +
   theme_minimal() +
   coord_flip() +
   scale_y_continuous(labels = scales::percent_format(scale = 100),
                     minor_breaks = NULL) +
   scale_x_discrete(labels = custom_labels ) +  # Apply custom labels to the x-axis
   labs(y = " ", x= " ")

Solution

  • You may use this (you need to set , position = position_dodge2(0.45) and group = Arviointi, in its aes()):

    You may use this:

    ggplot(plot_data, aes(x = Mittari, y = value, fill = Arviointi)) +
      geom_bar(stat = "identity", position = position_dodge2(reverse = TRUE), width = 0.45, alpha = 1) +
      geom_errorbar(data = plot_data, aes(x = Mittari, ymin = Kansallinen, ymax = Kansallinen, group = Arviointi,           
                                          width  = 0.45), position = position_dodge2(reverse = TRUE)) +    
      geom_text(aes(group = Arviointi, label = ("text here")), hjust=-0.5, colour = "black", position = position_dodge2(0.45, reverse = TRUE)) +
      scale_fill_manual(values = c("steelblue", "darkorange")) +
      scale_color_manual(values = c("steelblue", "darkorange")) +
      theme_minimal() +
      coord_flip() +
      scale_y_continuous(labels = scales::percent_format(scale = 100),
                         minor_breaks = NULL) +
      scale_x_discrete(labels = paste0("custom_labels",1:4) ) +  # Apply custom labels to the x-axis
      labs(y = " ", x= " ")
    

    enter image description here

    EARLIER ANSWER (before Q edit)

    ggplot(plot_data, aes(y = Mittari, x = value, group = Arviointi, fill = Arviointi)) +
      geom_bar(stat = "identity", position = position_dodge2(reverse = TRUE), width = 0.45, alpha = 1) +
      geom_errorbar(data = plot_data, aes(x = Mittari, ymin = Kansallinen, ymax = Kansallinen, group = Arviointi,           
                                          width  = 0.45), position = position_dodge2(reverse = TRUE)) +    
      geom_text(aes(group = Arviointi, label = label), hjust=-0.5, colour = "black", position = position_dodge2(reverse = TRUE, 0.45)) +
      scale_fill_manual(values = c("steelblue", "darkorange")) +
      scale_color_manual(values = c("steelblue", "darkorange")) +
      theme_minimal() +
      coord_flip() +
      scale_y_continuous(labels = scales::percent_format(scale = 100),
                         minor_breaks = NULL) +
      scale_x_discrete(labels = "custom_labels" ) +  # Apply custom labels to the x-axis
      labs(y = " ", x= " ")
    

    enter image description here

    Used data:

    plot_data <- data.frame(Mittari = c(20,5,58,60,70,62,75,80)/100,
                            value = rep(4:1, each = 2),
                            Arviointi = rep(c("A","B"), times = 4),
                            Kansallinen = rnorm(n = 8) + 1,
                            label = rep(c("text here A", "text here B"), 4)
                            )