mardi 14 janvier 2020

An ifelse statement in ggplot changes the colour of my legend

when I add an ifelse statement into a geom_rect command, the colour of the legend no longer matches the colours on the graph. Here is the code I've used:

data %>%
  ggplot(aes(xmin = Lift_Num - 0.5, xmax = Lift_Num + 0.5, ymin = Floor - 0.5, ymax = Floor + 0.5, alpha 
  = total_score)) +
  geom_rect(color = "grey", fill = ifelse(data$total_score == 0, "NULL", "red")) +
  theme_classic() +
  xlab("Lift Number") +
  ylab("Floor Number")

I want the graph to remove the NULL scores, but colour the others on a gradient red scale. The graph is in the red scale, but the the legend is a grey scale. It doesn't appear to be related to the 'grey' color fill (when changed to black the legend is still in grey), but does seem to be directly related to the ifelse statement as when removed the legend and graph match.

Aucun commentaire:

Enregistrer un commentaire