Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 204742

Change Line Colors Above Specific Values [duplicate]

$
0
0

This question already has an answer here:

library(ggplot2)
data = data.frame("person"=rep(1:2,4),
                  "time" = rep(1:4, 2),
                  "scores" = c(5,7,9,11,
                               8,12,13,14))
data = data[order(data$person),]

ggplot(data,aes(time,scores,group=as.factor(person))) +
  geom_line(color="red")

This code here will plot the two lines in red. I understand it how to change the color of a line. But~ how do I edit code so values of 8 and more are colored blue? THANK YOU eipi10.

eipi10 provided a great answer but I realize it only works when you have a single if statement, What if you have data such as

library(ggplot2)
data = data.frame("person"=c(rep(1,4), rep(2,4)),
                  "time" = rep(1:4, 2),
                  "scores" = c(5,7,9,11,
                               8,12,13,14),
                  "type" = c(1,2,2,3,
                             1,1,3,3))
data = data[order(data$person),]

And you want: 1. Red if type = 1 2. Blue if type = 2 3. Green if type = 3

Also I changed this question. The original is above and then I added this extra pieces which some could be very useful for.!


Viewing all articles
Browse latest Browse all 204742

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>