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")