如何根据不同的分位数更改 geom_quantile 中的线型?

How can I change linetype in geom_quantile based on different quantiles?

我想根据分位数更改 geom_quantile 中的线型以具有不同类型的线。添加 linetype = "" 将相同的线型应用于不同的分位数。相反,我想用实线表示 .5,虚线表示 .3 和 .7,虚线表示 .05 和 .95。

这是数据集的一部分:

df_long <- structure(list(year = c(1993, 1994, 1995, 1996, 1997, 1993, 1994, 
                               1995, 1996, 1997, 1993, 1994, 1995, 1996, 1997, 1993, 1994, 1995, 
                               1996, 1997, 1993, 1994, 1995, 1996, 1997, 1993, 1994, 1995, 1996, 
                               1997, 1993, 1994, 1995, 1996, 1997, 1993, 1994, 1995, 1996, 1997, 
                               1993, 1994, 1995, 1996, 1997, 1993, 1994, 1995, 1996, 1997, 1993, 
                               1994, 1995, 1996, 1997, 1993, 1994, 1995, 1996, 1997, 1993, 1994, 
                               1995, 1996, 1997, 1993, 1994, 1995, 1996, 1997, 1993, 1994, 1995, 
                               1996, 1997, 1993, 1994, 1995, 1996, 1997, 1993, 1994, 1995, 1996, 
                               1997, 1993, 1994, 1995, 1996, 1997, 1993, 1994, 1995, 1996, 1997, 
                               1993, 1994, 1995, 1996, 1997), value = c(-0.0738688893838062, 
                                                                        0.163757217900673, 0.0857009807383771, -0.0667526720322341, 0.0887060256107777, 
                                                                        -0.0705604326484671, -0.034097173504119, 0.111527592192091, -0.0457480764982465, 
                                                                        -0.021583994766979, 0.0315272708167682, 0.0677257599658057, 0.201360838487842, 
                                                                        -0.0439384872883349, 0.0352108660145989, 0.00290602695874642, 
                                                                        -0.0191047098517949, 0.154388472709404, -0.0551628136705012, 
                                                                        -0.0116829416335616, -0.00631796777141269, 0.128820227737596, 
                                                                        -0.0804450935804719, -0.0177009372848825, -0.0083312825335341, 
                                                                        -0.0823085797209683, -0.0123399865315431, 0.222205920508411, 
                                                                        -0.0242032678683269, -0.0170395037335201, -0.106064297340727, 
                                                                        0.0347329264648886, 0.074020242269053, -0.00572727991816724, 
                                                                        0.0210114931568262, -0.288679652994857, -0.0275640444206321, 
                                                                        0.0299901988583031, 0.298443737803506, 0.0658031220317905, 0.0682685773016796, 
                                                                        0.18732469463018, 0.088955508320856, 0.0530846554927398, -0.736970343793129, 
                                                                        0.111899139738996, -0.0133243118307523, -0.00943611596382643, 
                                                                        -0.0387819600157773, 3.35423479236764, -0.502363908141791, 0.0511261883422458, 
                                                                        0.0329065085527587, -0.0957237477272243, 0.0219330316850432, 
                                                                        -0.103146077645506, -0.0900595090357357, 0.0143041336251402, 
                                                                        -0.196830556041808, 0.15006983964848, -0.199215590911409, -0.391178850934326, 
                                                                        -0.309466944847281, -0.210074148976359, 0.0621269708298122, -0.141795064523149, 
                                                                        0.186715408723252, -0.0932781215854355, -0.252274661523447, 0.0311615049862106, 
                                                                        -0.118714420708426, 0.196978608015062, 0.0446075502804535, -0.183006199496033, 
                                                                        0.320569582819935, -0.281136431971191, 0.121160211180941, -0.124283126420151, 
                                                                        -0.285441899579401, 0.201538081023234, 0.333489583806246, 0.0489425412386162, 
                                                                        -0.15396195544482, 0.0904023603879698, -0.0620362904872416, 0.154384885949913, 
                                                                        0.00766253819373763, 0.147310771448641, -0.0824091765728805, 
                                                                        0.116736781380315, 0.0797858883570994, 0.445226197369783, -0.614739223178009, 
                                                                        0.103806438257921, -0.130146811666976, -0.438085244089705, 0.272598467933602, 
                                                                        0.0179854969797342, -0.211999005594562, 0.198933665513538)), row.names = c(NA, 
                                                                                                                                                   -100L), class = c("tbl_df", "tbl", "data.frame"))

我不知道如何在下面的代码中指定它:

    ggplot(aes(x=year, y=value)) +                       
    geom_jitter(size=3, alpha=0.35, width = .07) + 
    geom_quantile(quantiles = c(.05, .3, .5, .7, .95),
                  method = "rqss", lambda = .1,
                  colour = "red") +
    xlab("Year") +
    ylab("Percentage change") +
    theme_minimal()

添加 (aes(linetype = factor(..quantile..))) 得到五条不同的线,如下图所示。

如何才能只获得上面指定的 3 种不同类型的线?

请 post 您的数据(使用 dput 函数)以便我们可以 运行 编码。但我猜想你正在寻找的是将这种美学添加到你的 geom_quantile:

aes(linetype = factor(..quantile..))

要手动设置线型值,您可以执行以下操作:

scale_linetype_manual(values=c("dotted", "dashed", "solid", "dashed", "dotted"))

请注意,您是按照定义 quantiles 的顺序设置它们的。输出(图例):

要将图例更改为不重复线型:

scale_linetype_manual(breaks=c(.05, .3, .5), labels=c("0.05 or .95", "0.3 or 0.7", "0.5"),
    values=c("dotted", "dashed", "solid", "dashed", "dotted"))

输出(图例):

Obs:您可以通过添加 name="whatever".

来更改图例的名称