Home > Blockchain >  How to change the color of points on data plot?
How to change the color of points on data plot?

Time:03-11

I have a set of data, and I made a variable to show

data$win <- ifelse((data$distance>data$time), 1, 0)

Now I want to make a ggplot that had a legend and where if the points are 1 they show as green and if they are 0 the points show as red

ggplot(data, aes(x=time, y=distance, col=factor(win))) geom_point()

Here it shows everything I want, the legend and the plotted points, but how do I get my dots to be green and red? right now they are blue and orange

CodePudding user response:

As Jon Spring pointed out, scale_colour_manual() is the way to do this. Here is an example:

library(ggplot2)
library(dplyr)

mtcars %>%
  mutate(win = ifelse(cyl <= 4, 1, 0)) %>%
  ggplot(aes(x = wt, y = mpg, col = factor(win)))  
  geom_point()  
  scale_colour_manual(values = c("1" = "green",
                                 "0" = "red"))

Created on 2022-03-11 by the reprex package (v2.0.1)

CodePudding user response:

In your case, the following code should also work

ggplot(data, aes(x=time, y=distance, col=distance>time))   geom_point()
  • Related