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

636 views Asked by At

I would like to change the line type in geom_quantile to have different types of line based on the quantile. Adding linetype = "" applies the same line type to different quantiles. Instead, I would like to have a solid line for .5, dashed line for .3 and .7 and dotted line for .05 and .95.

Here's part of the dataset:

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

I don't know how to specify it in the following code:

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

Adding (aes(linetype = factor(..quantile..))) gives me five different lines, like in the following picture.

How can I get only 3 different types of line as specified above?

1

There are 1 answers

2
Ricardo Semião On BEST ANSWER

Please post your data (use dput function) so we can run code with. But i can guess that what you're looking for is adding this aesthetic to your geom_quantile:

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

To manually set the values of line type you can do:

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

Note that you set them by the order of how you defined quantiles. Output (legend):

enter image description here

To change the legend to not repeat line types:

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

Output (legend):

enter image description here

Obs: you can change the name of the legend by adding a name="whatever".