how to change the color in geom_point or lines in ggplot [duplicate]

Solution 1:

You could use scale_color_manual:

ggplot() + 
  geom_point(data = data, aes(x = time, y = y, color = sample),size=4) +
  scale_color_manual(values = c("A" = "black", "B" = "red"))

enter image description here


Per OP's comment, to get lines with the same color as the points you could do:

ggplot(data = data, aes(x = time, y = y, color = sample)) + 
  geom_point(size=4) + 
  geom_line(aes(group = sample)) + 
  scale_color_manual(values = c("A" = "black", "B" = "red"))

enter image description here

Solution 2:

I would do it like this (you can also use hexidecimal colors instead of red, black)

data <- data %>% 
  mutate(Color = ifelse(sample == "A", "black",
                 ifelse(sample == "B", "red", "none")))

ggplot() + 
  geom_point(data = data, aes(x = time, y = y, color = Color),size=4)+
  scale_color_identity()