1

I have a question with ggplot2 in R. I can draw two lines with ggplot2, how to set different colors for different parts in each line?

I have drawn two lines for both B1_UNAV and B1_CNAV, for both line, there are also two types: 0 and 11. How to set different colors for each type? I have written some code:

library(ggplot2)    
p <- ggplot(data = B1.m, mapping = aes(x = as.Date(B1.m$B1_Date, format='%d/%m/%Y'), y = B1.m$B1_Value, color = B1.m$B1_Type)) + geom_line()
p <- p + labs(x = "Date", y = "Value") + labs(color = "Value Type")

It seems that color in aes has already separate two lines for B1_UNAV and B1_CNAV. How to separate 0 and 11 type with different line colors? **The key point is that I want to have different color in different parts of same line (which means at two lines, there may be more classifications.)**My data is as followed:

B1.m <- structure(list(B1_Date = structure(c(8L, 3L, 1L, 10L, 9L, 7L, 
6L, 5L, 4L, 2L), .Label = c("01/06/2018", "08/05/2018", "08/06/2018", 
"09/05/2018", "10/05/2018", "11/05/2018", "14/05/2018", "15/06/2018", 
"18/05/2018", "25/05/2018"), class = "factor"), B1_Value = c(1.033, 
1.032, 1.0322, 1.0319, 1.0299, 1.0302, 1.03, 1.0299, 1.0297, 
1.0299), NewGroup = c(" 0_B1_UNAV", " 0_B1_UNAV", " 0_B1_UNAV", 
" 0_B1_UNAV", " 0_B1_UNAV", "11_B1_UNAV", "11_B1_UNAV", "11_B1_UNAV", 
"11_B1_UNAV", "11_B1_UNAV")), .Names = c("B1_Date", "B1_Value", 
"NewGroup"), row.names = c(NA, 10L), class = "data.frame")
Ericshaw
  • 51
  • 6
  • 1
    see this post to learn how to provide a [minimum reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). The dataset should never be posted in image format. Besides, have you seen these similar posts, [1](https://stackoverflow.com/questions/5171263/changing-line-colors-with-ggplot), [2](https://stackoverflow.com/questions/43770579/how-to-change-the-color-in-geom-point-or-lines-in-ggplot). If yes, and no solution then do edit your question and provide both a minimum reproducible example and what is unique in your problem. – mnm Jun 18 '18 at 03:08
  • Also: you do not need to use `$` to specify columns inside of `aes()`. Just use the column name _e.g._ `aes(color = B1_Type)`. – neilfws Jun 18 '18 at 03:13

1 Answers1

0

If you want to divide your plot by 0 and 11 types rather than B1_UNAV and B1_CNAV, just change your aesthetics argument to color = B1_PR(and remove B1.m from your arguments inside aes) like this:

p <- ggplot(data = B1.m, mapping = aes(x = as.Date(B1_Date, format='%d/%m/%Y'), 
                                       y = B1_Value, color = B1_PR)) + 
       geom_line()

If, on the other hand, you want to create groupings based on combinations of B1_Type and B1_PR, then you'll need to create a new grouping factor, and set the color argument to that. Something like this should work to create a new grouping factor:

B1.m$NewGroup <- paste0(c(B1_PR, B1_Type), sep="_")
p <- ggplot(data = B1.m, mapping = aes(x = as.Date(B1_Date, format='%d/%m/%Y'), 
                                       y = B1_Value, color = NewGroup)) + 
       geom_line()
phalteman
  • 3,442
  • 1
  • 29
  • 46