如何根据不同的分位数更改geom_quantile中的线型? [英] How can I change linetype in geom_quantile based on different quantiles?

查看:86
本文介绍了如何根据不同的分位数更改geom_quantile中的线型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是数据集的一部分:

  df_long<-结构(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),值= 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),类别= c("tbl_df","tbl","data.frame"))) 

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

  ggplot(aes(x = year,y = value))+geom_jitter(大小= 3,alpha = 0.35,宽度= .07)+geom_quantile(quantiles = c(.05,.3,.5,.7,.95),方法="rqss",lambda = 0.1,颜色=红色")+xlab(年份")+ylab(百分比变化")+theme_minimal() 

添加(aes(linetype = factor(.. quantile ..)))给了我五行不同的内容,如下图所示.

要更改图例以不重复线型:

  scale_linetype_manual(breaks = c(.05,.3,.5),labels = c("0.05或.95","0.3或0.7","0.5"),值= c(虚线",虚线",实心",虚线",虚线"))) 

输出(旧版):

糟糕:您可以通过添加 name ="whatever" 来更改图例的名称.

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?

解决方案

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

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

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

这篇关于如何根据不同的分位数更改geom_quantile中的线型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆