Home > Back-end >  Changing colors of variables on a plot
Changing colors of variables on a plot

Time:04-27

I am trying to change the color for the individual 'Years' variable on this plot. So far I have this code:

x <- ggplot(elect, aes(x = `Congressional Spending`, y = `United States VEP Midterm Turnout Rate`, color = Year))  
     geom_point()   geom_smooth(method = lm, col = 'purple', size = 0.5)

I also tried using:

scale_color_manual(values = c("red", "yellow",
                               "blue", "orange",
                               "green", "black"))

Error: Continuous value supplied to discrete scale

CodePudding user response:

It would be helpful to have a look at the data. The problem is most likely that your variable year is a double. So you need to call as.factor() in your code (see below):

library(tidyverse)
ggplot(mtcars, aes(x=gear, y=carb, color=as.factor(cyl)))  
  geom_point()  
  scale_color_manual(values = c("black","green","blue"))

Created on 2022-04-27 by the reprex package (v2.0.0)

  • Related