I have the following data:
structure(list(Date = c("01.08.2018", "02.08.2018", "03.08.2018",
"04.08.2018", "01.09.2018", "02.09.2018", "03.09.2018", "04.09.2018",
"01.08.2018", "02.08.2018", "03.08.2018", "04.08.2018", "01.09.2018",
"02.09.2018", "03.09.2018", "04.09.2018", "01.08.2018", "02.08.2018",
"03.08.2018", "04.08.2018", "01.09.2018", "02.09.2018", "03.09.2018",
"04.09.2018"), Return = c(0.1, 0.4, 0.6, 0.2, 0.5, 0.3, 0.2,
0.5, 0.2, 0.5, 0.2, 0.4, 0.5, -0.3, 1, -0.4, 0.4, 0.3, -0.3,
0.2, 0.1, 0.3, 0.5, 0.6), Rating = c(1L, 1L, 2L, 2L, NA, NA,
4L, 5L, 5L, 3L, 2L, NA, 2L, 1L, 1L, 1L, 2L, 2L, NA, 3L, 3L, 3L,
4L, 4L)), class = "data.frame", row.names = c(NA, -24L))
I would like to use ggplot to have a plot with the Return on the y-axis and the Date on the x-axis. The lines in the plot should be from the 6 different Ratings (1,2,3,4,5 and NA). How can I change the line from the NA Rating to a dashed line and keep the other lines from the Ratings 1 to 5 solid?
I have tried the following:
ggplot(Data, aes(x=`Date`, y=`Return`, group=`Rating`, fill=`Rating`, color=`Rating`))
geom_line(size=1)
scale_color_brewer(palette="Paired")
My main problem is how to get the dashed line for the group with Rating NA. What do I need to add to the code?
Many thanks in advance!!
CodePudding user response:
You can put Rating
in the linetype
in your aes
, to then use scale_linetype_manual
to set Na to dashed:
Data <- Data %>%
mutate(Rating = fifelse(is.na(Rating),"NA",as.character(Rating)))
ggplot(Data, aes(x= Date, y= Return,group = Rating, color= Rating,linetype = Rating))
geom_line(size=1)
scale_linetype_manual(breaks = c("NA",1:5),values = c("dashed",rep("solid",5)))