Search code examples
rggplot2dplyrgeom-text

Error in eval_tidy(pair$lhs, env = default_env) : object 'Var1' not found


I am having trouble using case_when for customizing my labels in a ggplot inside a pipe chain.

I'm working with labbeled data but I made this reproducible data to show my error. Here's my code:

#data
padmin1<- data.frame(q0005_0001 = rep(c("Insuficiente1", "Poco Suficiente2","Regular3","Suficiente4","Muy Suficiente5")),5)

#Graphic
padmin1 %>% 
  rename(Var1=q0005_0001) %>% 
  ggplot(aes(x = "", y = X5, fill = fct_rev(ordered(Var1)))) +  
  geom_bar(stat = "identity", width = 0.2) +
  geom_text(aes(label = X5), position = position_stack(vjust=0.5), colour= case_when(
    Var1 == "Insuficiente1" ~ "white",
    Var1 == "Poco Suficiente2" ~ "black",
    Var1 == "Regular3" ~ "black",
    Var1 == "Suficiente4" ~ "white",
    Var1 == "Muy Suficiente5" ~ "white",
    TRUE ~ "white"
  ) , fontface = "bold") +
  coord_flip() +
  labs(title= "La información brindada por la facultad le resultó...", caption = "Elaborado por SS, 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()) +
  scale_fill_manual("Leyenda", values = c("Insuficiente1"="#8A0000", "Poco Suficiente2"="#FFCD2F", "Regular3"="#DAA600", "Suficiente4"="#144D6C", "Muy Suficiente5"="#071C27")) 

After running this code it appears the following error:

#Error in eval_tidy(pair$lhs, env = default_env) : object 'Var1' not found

Let me know what is wrong, i already tried puting .$ before Var1 in the geom_text() but is not working.


Solution

  • To achieve your desired result move colour=case_when(... inside aes() and add scale_color_identity. However, instead of making use of a case_when I would suggest an ifelse + grepl as there are just two options, black and white.

    Moreover, to the get right order of the labels you have to add group=fct_rev(ordered(Var1)). Otherwise the labels are "stacked" in a different order than the bars:

    #data
    padmin1<- data.frame(q0005_0001 = rep(c("Insuficiente1", "Poco Suficiente2","Regular3","Suficiente4","Muy Suficiente5")),5)
    
    library(ggplot2)
    library(dplyr)
    library(forcats)
    
    #Graphic
    padmin1 %>% 
      rename(Var1=q0005_0001) %>% 
      ggplot(aes(x = "", y = X5, fill = fct_rev(ordered(Var1)))) +  
      geom_bar(stat = "identity", width = 0.2) +
      geom_text(aes(label = X5, 
                    color = ifelse(grepl("^(P|R)", Var1), "black", "white"),
                    group = fct_rev(ordered(Var1)) ), position = position_stack(vjust=0.5) , fontface = "bold") +
      scale_color_identity() +
      coord_flip() +
      labs(title= "La información brindada por la facultad le resultó...", caption = "Elaborado por SS, 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()) +
      scale_fill_manual("Leyenda", values = c("Insuficiente1"="#8A0000", "Poco Suficiente2"="#FFCD2F", "Regular3"="#DAA600", "Suficiente4"="#144D6C", "Muy Suficiente5"="#071C27"))