0

I would like to control the color for lines in my plotting, however I ran into some difficulty. The lines are by PARAMCD. There is NA in PARAMCD. In order to control legend, I used scale_color_discrete. But when I try to control color again, it won't let me do it. I am sure I am doing it wrong, maybe defined color too many times. I am not familiar with how to control the color. Please pardon me for such messy codes.

I codes I used to do plotting are:

ggplot(data =df)+
       geom_line(aes(x = ADY, y = AVAL, color = PARAMCD, yaxs="d", xaxs="d"))+
       geom_point(aes(x = ADY, y = AVAL))+
       scale_color_discrete(breaks=c("SYSBP", "DIABP", "PULSE"),name = "Vital signs", labels = c("Systolic BP", "Diastolic BP",  "Pulse"))+
       scale_colour_manual(values=c(DIABP="#512d69",SYSBP="#007254",PULSE="#fd9300"))

The output should be something that looks like this but I want the color to be green, orange and purple.

enter image description here

Could someone give me some guidance on this? Thanks.

The sample data can be build using codes:

df<- structure(list(ADY = c(-6, -6, -6, 1, 1, 1, 8, 8, 8, 15, 15, 
15, 22, 22, 22, 29, 29, 29, 43, 43, 43, 57, 57, 57, 64, 87, 87, 
87, 101, 101, 101), AVAL = c(66, 67, 127, 70, 58, 136, 68, 74, 
140, 145, 74, 58, 75, 72, 149, 82, 66, 143, 86, 60, 159, 64, 
87, 136, NA, 73, 58, 135, 141, 74, 74), PARAMCD = structure(c(3L, 
1L, 2L, 1L, 3L, 2L, 3L, 1L, 2L, 2L, 1L, 3L, 1L, 3L, 2L, 1L, 3L, 
2L, 1L, 3L, 2L, 3L, 1L, 2L, NA, 1L, 3L, 2L, 2L, 1L, 3L), .Label = c("DIABP", 
"SYSBP", "PULSE"), class = "factor")), row.names = c(NA, -31L
), class = "data.frame")
markus
  • 25,843
  • 5
  • 39
  • 58
Stataq
  • 2,237
  • 6
  • 14
  • 1
    Start with `ggplot(na.omit(df), aes(x = ADY, y = AVAL)) + geom_line(aes(color = PARAMCD)) + geom_point()` - you will not need the line `scale_discrete(...` – markus Jan 26 '21 at 20:34
  • 1
    Related: [Eliminating NAs from a ggplot](https://stackoverflow.com/questions/17216358/eliminating-nas-from-a-ggplot) – markus Jan 26 '21 at 20:35
  • If I removed the last row, the color are not what I wanted. If I keep it, the color are right ,but the legend is not defined as I want. What should I do. I think I must define too many times color. – Stataq Jan 26 '21 at 20:35

1 Answers1

2

Try this:

ggplot(data = subset(df,!is.na(df$PARAMCD)),
       aes(x = ADY, y = AVAL, color = PARAMCD)) +
  geom_line() +
  geom_point() +
  scale_colour_manual(values = c(
    DIABP = "#512d69",
    SYSBP = "#007254",
    PULSE = "#fd9300"
  ),labels = c("Systolic BP", "Diastolic BP",  "Pulse"))

enter image description here

markus
  • 25,843
  • 5
  • 39
  • 58
Leonardo
  • 2,439
  • 33
  • 17
  • 31