Home > database >  Percentages in the wrong position in ggplot2
Percentages in the wrong position in ggplot2

Time:01-29

I'm trying to plot a graph for a likert test using ggplot2 and I would like to have the percentages values appearing on the graph. I've created a df with all the averages and percentages so I could write it on the graph. It all seems to be working good, except the values are being plotted as if they were upsided or something.

This is the code I'm using

    example <- structure(list(grupo = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("EJA", 
"REG"), class = "factor"), nivel = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("CINCO", "DOZE", "NOVE"), class = "factor"), tipo = structure(c(1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 3L), .Label = c("COR", "PAD", "RES"), class = "factor"), 
    likert = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
    3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L), .Label = c("0", 
    "1", "2", "3"), class = c("ordered", "factor")), cnt = c(3L, 
    1L, 3L, 5L, 3L, 1L, 3L, 6L, 2L, 1L, 10L, 5L, 5L, 9L, 11L, 
    6L, 4L, 10L, 10L, 10L), freq = c(0.25, 0.083, 0.25, 0.417, 
    0.231, 0.077, 0.231, 0.462, 0.154, 0.077, 0.769, 0.167, 0.167, 
    0.3, 0.367, 0.2, 0.133, 0.333, 0.333, 0.333), prop = c(25, 
    8.3, 25, 41.7, 23.1, 7.7, 23.1, 46.2, 15.4, 7.7, 76.9, 16.7, 
    16.7, 30, 36.7, 20, 13.3, 33.3, 33.3, 33.3), proptext = c("25", 
    "8.3", "25", "41.7", "23.1", "7.7", "23.1", "46.2", "15.4", 
    "7.7", "76.9", "16.7", "16.7", "30", "36.7", "20", "13.3", 
    "33.3", "33.3", "33.3")), row.names = c(NA, -20L), groups = structure(list(
    grupo = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("EJA", 
    "REG"), class = "factor"), nivel = structure(c(1L, 1L, 1L, 
    2L, 2L, 2L), .Label = c("CINCO", "DOZE", "NOVE"), class = "factor"), 
    tipo = structure(c(1L, 2L, 3L, 1L, 2L, 3L), .Label = c("COR", 
    "PAD", "RES"), class = "factor"), .rows = structure(list(
        1:4, 5:8, 9:11, 12:15, 16:19, 20L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))
    
    ggplot(example, aes(x=(interaction(grupo, nivel, tipo)),y=prop, fill=likert)) 
      geom_col() 
      #scale_y_continuous(labels = percent) 
      coord_flip()  
      ggtitle("Testing") 
      xlab("A, B, and C") 
      ylab("%") 
      geom_text(aes(label = proptext), size = 2, colour = "black")

Would someone have an idea of how could I solve it?

Thanks!

CodePudding user response:

The geom_text may also require the x, y

library(dplyr)
library(tidyr)
library(ggplot2)
example %>%    
  unite(new, grupo, nivel, tipo, sep = ".") %>% 
  ggplot(aes(x=new, fill=likert)) 
    geom_col(aes(y= prop)) 
    geom_text(aes(x = new, y = prop, label = proptext),
     position = position_stack(vjust = .5))   
  coord_flip()  
  #scale_y_continuous(labels = percent) 
  
  ggtitle("Testing") 
  xlab("A, B, and C") 
  ylab("%")

-output

enter image description here

  • Related