我有以下資料:
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))
我想使用 ggplot 在 y 軸上繪制 Return 并在 x 軸上繪制 Date。圖中的線條應來自 6 個不同的評級(1、2、3、4、5 和 NA)。如何將 NA 評級的線更改為虛線,并使評級 1 到 5 的其他線保持實線?
我嘗試了以下方法:
ggplot(Data, aes(x=`Date`, y=`Return`, group=`Rating`, fill=`Rating`, color=`Rating`))
geom_line(size=1)
scale_color_brewer(palette="Paired")
我的主要問題是如何獲得評級為 NA 的組的虛線。我需要在代碼中添加什么?
提前謝謝了!!
uj5u.com熱心網友回復:
您可以將Rating,linetype放入aes,然后使用scale_linetype_manual將 Na 設定為虛線:
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)))

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/466736.html
