0

I'm plotting a cross-level interaction, but the legend shows all of the scores, independently, instead of in a ranged colored scale, as the ones we usually find in heatmaps. How can I get the information from my clustering variable 'w' to show in a more parsimonious legend (i.e., only one bar with the full range of colors, and a couple of score-markers)?

This is my code, and what the graph currently looks like

Thanks in advance!

graph2 <- ggplot(data = data1a,
       aes(x = x2, 
           y = y, 
           col = as.factor(w)))+
  viridis::scale_color_viridis(discrete = TRUE)+
  geom_point(size     = .7,
             alpha    = .8, 
             position = "jitter")+
  geom_smooth(method = lm,
              se     = FALSE,
              size   = .5,
              alpha  = .8)+
  theme_minimal()+
  labs(title    = "Linear Relationship for Different wards as Observed", 
       subtitle = "", 
       col      = "Years of\nTeacher\nExperience")

graph2

Interaction graph in GGplot2

  • 1
    I guess it is the `col = as.factor(w)` , try it without the factor. It would be helpful, if you make a [minimum, reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). – MarBlo Jul 11 '20 at 04:08
  • @MarBlo Yes, and also to set `discrete = TRUE` in `scale_color_viridis`. The OP cannot have a *"ranged colored scale"* and define a discrete color scale twice. – Rui Barradas Jul 11 '20 at 04:22

1 Answers1

0

Replace your viridis::scale_color_viridis(discrete = TRUE) with scale_color_gradient(low = "blue", high = "green").

YBS
  • 19,324
  • 2
  • 9
  • 27