How to use a column as legend values in ggplot2 R and column attribute as title

128 Views Asked by At

I have this data:

dput(padmin1)

structure(list(q0005_0001 = structure(c(5, 4, 3, 3, 4, 4, NA, 
3, NA, 4, 4, 4, 3, 4, 4, 4, 3, 4, 3, 4, NA, 3, 5, 4, NA, 4, 4, 
3, 3, 4, 4, NA, 5, 5, 5, 3, 5, 4, 4, 4, 4, 4, 4, 5, 3, 3, 5, 
2, 4, NA, 3, 4, 4, 5, 5, 3, 4, 3, 3, 4, 5, 4, 3, 4, 4, 4, 4, 
3, 4, 4, 5, 4, 5, NA, 4, 4, NA, 3, 4, 4, 5, NA, 5, 4, 4, 4), format.spss = "F8.2", class = c("haven_labelled", 
"vctrs_vctr", "double"), labels = c(`No reciboinformación` = 0, 
Insuficiente1 = 1, `Poco Suficiente2` = 2, Regular3 = 3, Suficiente4 = 4, 
`Muy Suficiente5` = 5), label = "La información brindada por la facultad le resultó...")), row.names = c(NA, 
-86L), class = c("tbl_df", "tbl", "data.frame"), label = "File created by user 'asyncjobs_user' at Mon Jul  5 19:01:40 202")

and the following code:

padmin1 %>% 
  #Ordenando la data
  rename(Var1=q0005_0001) %>%
  group_by(Var1) %>% 
  select(Var1) %>% 
  haven::as_factor() %>% 
  table() %>% 
  as.data.frame() %>% 
  mutate(Freq = round(100 * Freq/sum(Freq), 0),
         Colores = c( "#7F7F7F", "#8A0000", "#FFCD2F", "#DAA600", "#144D6C", "#071C27")) %>% 
  filter(Freq != 0) %>% 
  #Para el gráfico
  ggplot(aes(x = "", y = Freq, fill = Colores  )) +  
  scale_fill_identity() +
  geom_bar(stat = "identity", width = 0.2) +
  #Texto
  geom_text(aes(label = paste0(Freq, "%"), 
                color = ifelse(grepl("^(#7F7F7F|#8A0000|#FFCD2F)", Colores), "black", "white"),
                group = fct_rev(ordered(.)) ), position = position_stack(vjust=0.5) , fontface = "bold") +
  scale_color_identity() +
  #Rotar gráfico
  coord_flip() +
  #Título y pie de página
  labs(title= "La información brindada por la facultad le resultó...", caption = "Enterprise, 2021") + 
  #Temas de colores
  theme(axis.title = element_blank(), 
        line = element_blank(),
        panel.background = element_rect(fill = "transparent", color = NA),
        plot.background = element_rect(fill = "transparent", color = NA),
        legend.position = "bottom", 
        panel.border = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.background = element_rect(fill = "transparent", linetype = "solid", colour = "transparent"),
        legend.box.background = element_rect(fill = "transparent", colour = "transparent"),
        axis.text = element_blank()) 


Which gives this output: barplot

Request:

  1. I would like to use values (Poco Suficiente2, Regular3, Suficiente4, Muy Suficiente5) as legend in the buttom without sacrificing the HEX colors because its important to me that colors in that order.

  2. My second request is to use the attribute of the column as the title of the graphic because i'm working with labelled data imported from SPSS.

Additional notes:

*I'm looking forward to make this graphic as automatic as possible from the data thats why i'm using the dplyr's pipe system to create a table and work from there, so the solution should go from there and not create additional objects.

*Finally i would like the output to look something like this but with the given data specified at the beggining and with the colors of the number labels of the first graphic (black for light bar colors and white for dark bar colors): barplot2

Thanks very much Stackoverflow community, i look forward to your feedback. Feel free to ask me for more details. Peace

0

There are 0 best solutions below