更改ggplot图例中的线型 [英] Changing the line type in the ggplot legend

查看:914
本文介绍了更改ggplot图例中的线型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个df:

 头(hej3)

年变量N总和平均值sd差异中值最小值最大值
1 1901Delägare.män。 85 18089 212.81176 365.39168 133511.083 110.0 5 2771
2 1901Delägare.kvinnor。 48 3509 73.10417 97.84209 9573.074 34.5 1 414
3 1902Delägare.män。 92 19783 215.03261 363.63529 132230.625 111.5 2 2827
4 1902Delägare.kvinnor。 53 3872 73.05660 94.12827 8860.131 40.0 1 408
5 1903Delägare.män。 99 21123 213.36364 366.58411 134383.907 109.0 2 2994
6 1903Delägare.kvinnor。 55 4201 76.38182 93.85006 8807.833 40.0 1 390

dput(hej3)

结构(列表(年份= C(1901L,1901L,1902L,1902L,1903L,1903L,
1904L,1904L,1905L,1905L,1906L,1906L,1907L,1907L,1908L,
1908L,1909L,1909L,1910L,1910L,1911L,1911L,1912L,1912L,
1915L,1915L, 1921L,1921L,1924L,1924L,1927L,1927L,1930L,
1930L),可变=结构(C(1L,2L,1L,2L,1L,2L,1L,2L,
1L,2L ,1L,2L,1L,2L,1L,2L,1L,2L,1L,2L,1L,2L,1L,2L,
1L,2L,1L,2L,1L,2L,1L,2L,1L ,2L),.Label = c(Delägare.män。,
Delägare.kvinnor。),class =factor),N = c(85,48,92,53,
99,55,112,63,116,69,126,76,130,78,130,80,129,83,
125,81,118,77,97,72,50,41,42 ,34,42,33,26,20,29,
29),Sum = c(18089,3509,19783,3882,2123123,4201,23686,5087,$ b $ 26751,5652,28198, 6493,31638,6934,32611,7194,36133,7025,
30138,6884,26935,6900,20333,8021,18929,5619,11157,4332,
10778,4437,7974,3416,9270,3773),平均值= c(212.811764705882,
73.1041666666667,215.032608695652,73.0566037735849,213.363636363636,
76.3818181818182,211.482142857143,80.7460317460317,230.612068965517,
81.9130434782609 ,223.793650793651,85.4342105263158,243.369230769231,
88.8974358974359,250.853846153846,89.925,280.100775193798,
84.6385542168675,241.104,84.9876543209877,228.262711864407,
89.6103896103896,209.618556701031,111.402777777778,378.58,
137.048780487805,265.642857142857 ,127.411764705882,256.619047619048,
134.454545454545,306.692307692308,170.8,319.655172413793,
130.793103448276),SD = C(365.391684625249,97.8420871855394,
363.635291602196,94.1282707255493,366.5841066326,93.8500559223754,
373.650556559185, 106.929577104772,405.688052605677,109.41727188241,
421.942750950132,110.801123403007,462.951922738037,115.87931358968,
502.602700547356,117.741378786224,642.0432 93966629,114.535815924939,
459.068496259615,114.82317860815,360.246791665663,119.967995276389,
293.702281347504,224.695704072853,460.551137890511,162.282154166672,
231.68690199813,139.634830604701,226.30617641151,143.124310240498,
343.058102277823,181.389548819806,410.53721563181, 192.111645239046
)中,差异= C(133511.083193277,9573.0740248227,132230.625298614,
8860.13134978229,134383.907235622,8807.832996633,139614.738416988,
11433.9344598054,164582.796026986,11972.1393861893,178035.685079365,
12276.8889473684,214324.482766846,13428.0153180153 ,252609.474597496,
13863.032278481,412219.591327519,13118.4531295915,210743.884258065,
13184.362345679,129777.750905403,14392.3198906357,86261.0300687285,
50488.159428795,212107.350612245,26335.4975609756,53678.8205574913,
19497.8859180036,51214.4854819977,20484.5681818182,117688.861538462 ,
32902.1684210526,168540.805418719,36906.88中值= c(110,
34.5,111.5,40,109,40,112,47,109.5,34,111.5,35,120.5,
41.5,124.5,46.5,125,44 ,124,44,121,44,112,42.5,251,
,85,199.5,93.5,186,88,206,111,185,50),Min = c(5,1,2,
1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,
19, 1,19,1,19,1,49,5,0,0),Max = c(2771,414,2827,408,
2994,390,3196,506,3421,524,3680,553 ,3952,570,4271,
581,4585,601,3898,602,2603,592,1951,1699,2269,675,
1151,576,1122,565,1680,652,2086 ,809)),.Names = c(Year,
variable,N,Sum,Mean,sd,Variance,Median,Min
Max),row.names = c(NA,-34L),class =data.frame)

$ b $

  theplot < -  ggplot(subset(hej3,variable == (y = Mean,color =Medelvärde),
size = 1.5,linetype = 1,aes(x = Year))+

geom_line alpha = 1)+

geom_ribbon(aes (ymax = Max,
ymin = Min,fill =DelägareMänMax / Min),linetype = 3,
alpha = 0.4)+

geom_ribbon(aes(ymax = Mean + sd,ymin = Mean-sd,fill =Mean + - sd),
color =grey50,linetype = 3,alpha = 0.8)+

#geom_line (aes(y = Sum,
#color =SumDelägareMän),size = 0.9,linetype = 1,alpha = 1)+

geom_line(aes(y = N,
color =Antal Kassor),size = 0.9,linetype = 2,alpha = 1)+

scale_y_continuous(breaks = seq(-500,4800,by = 100) = c(-500,4800),
labels = seq(-500,4800,by = 100))+
scale_x_continuous(break = seq(1901,1930,2))+
实验室(标题= ManligaDelägare我Yrkeskassor)+

scale_color_manual( Variabler,断裂= C( 安塔尔Kassor, Medelvärde),
值= C(安塔尔Kassor = 黑, Medelvärde= #6E6E6E))+
scale_fill_manual( 丝带,符= C( Delägare男人最大/最小, 中庸+ - SD),
值= c(`DelägareMänMax / Min` =grey50,`Mean + - sd` =#4E4E4E))+
theme(legend.direction =horizo​​ntal,legend.position =底部,legend.key = element_blank(),
legend.background = element_rect(fill =white,color =gray30))+
guides(fill = guide_legend(keywidth = 0.9,keyheight = 1))


我有 2个问题


  1. 如何更改图例中的线条类型?我希望图中的Antal Kassor 变量像图中一样虚线(linetype = 2)。


  2. 我如何放大y轴的一部分?我想放大y轴-300和600之间的区域。


我知道这里有很多ggplot向导:)谢谢!

解决方案

geom_line()>的调用中,放置 linetype = aes()中,并将该类型设置为变量名称。

  size = 1.5,alpha = 1)
+ geom_line(aes(y = N,
color =Antal Kassor,linetype =Antal Kassor),size = 0.9,alpha = 1)

然后,您应该添加 scale_linetype_manual(),其名称与 scale_colour_manual()

  + scale_linetype_manual(Variabler,values = c(Antal Kassor = 2,Medelvärde= 1))

guides()应该调整为线型颜色以更好地显示图例中的线条。



$ $ p $ + guides(fill = guide_legend(keywidth = 1,keyheight = 1),
linetype = guide_legend(keywidth = 3,keyheight = 1),
color = guide_legend(keywidth = 3,keyheight = 1))

< img src =https://i.stack.imgur.com/xahsc.jpgalt =

这里使用完整的代码:

 绘图< -  ggplot(subset(hej3,variable ==Delägare.män。),aes(x = Year) )+ 

geom_line(aes(y = Mean,color =Medelvärde,linetype =Medelvärde),
size = 1.5,alpha = 1)+

geom_ribbon(aes(ymax = Max,
ymin = Min,fill =D最大/最小),线型= 3,
alpha = 0.4)+

geom_ribbon(aes(ymax =平均值+ sd,ymin =平均值 - 填充=平均值+ - sd),
color =grey50,linetype = 3,alpha = 0.8)+

#geom_line(aes(y = Sum,
#color =Sum DelägareMän),size = 0.9,linetype = 1,alpha = 1)+

geom_line(aes(y = N,
color =Antal Kassor,linetype =Antal Kassor ),size = 0.9,alpha = 1)+

scale_y_continuous(break = seq(-500,4800,by = 100),limits = c(-500,4800),
labels = seq(-500,4800,by = 100))+
scale_x_continuous(breaks = seq(1901,1930,2))+
labs(title =ManligaDelägarei Yrkeskassor)+

scale_color_manual(Variabler,breaks = c(Antal Kassor,Medelvärde),
values = c(Antal Kassor=black,Medelvärde=# 6E6E6E))+
scale_fill_manual(Ribbons,breaks = c(DelägareMänMax / Min,Mean + - sd),
values = c(`DelägareMänMax / Min` =grey50,`Mean + - sd` =#4E4E4E))+
scale_linetype_manual(Variabler,值= c(Antal Kassor= 2,Medelvärde= 1))+
主题(legend.direction =horizo​​ntal,legend.position =bottom,legend.key = element_blank(),
legend.background = element_rect(fill =white,color =gray30))+
guides(fill = guide_legend(keywidth = 1,keyheight = 1),linetype = guide_legend(keywidth = 3, keyheight = 1),
color = guide_legend(keywidth = 3,keyheight = 1))+
coord_cartesian(ylim = c(-300,600))


I have a df:

head(hej3)

  Year          variable  N   Sum      Mean        sd   Variance Median Min  Max
1 1901     Delägare.män. 85 18089 212.81176 365.39168 133511.083  110.0   5 2771
2 1901 Delägare.kvinnor. 48  3509  73.10417  97.84209   9573.074   34.5   1  414
3 1902     Delägare.män. 92 19783 215.03261 363.63529 132230.625  111.5   2 2827
4 1902 Delägare.kvinnor. 53  3872  73.05660  94.12827   8860.131   40.0   1  408
5 1903     Delägare.män. 99 21123 213.36364 366.58411 134383.907  109.0   2 2994
6 1903 Delägare.kvinnor. 55  4201  76.38182  93.85006   8807.833   40.0   1  390

dput(hej3)

structure(list(Year = c(1901L, 1901L, 1902L, 1902L, 1903L, 1903L, 
1904L, 1904L, 1905L, 1905L, 1906L, 1906L, 1907L, 1907L, 1908L, 
1908L, 1909L, 1909L, 1910L, 1910L, 1911L, 1911L, 1912L, 1912L, 
1915L, 1915L, 1921L, 1921L, 1924L, 1924L, 1927L, 1927L, 1930L, 
1930L), variable = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("Delägare.män.", 
"Delägare.kvinnor."), class = "factor"), N = c(85, 48, 92, 53, 
99, 55, 112, 63, 116, 69, 126, 76, 130, 78, 130, 80, 129, 83, 
125, 81, 118, 77, 97, 72, 50, 41, 42, 34, 42, 33, 26, 20, 29, 
29), Sum = c(18089, 3509, 19783, 3872, 21123, 4201, 23686, 5087, 
26751, 5652, 28198, 6493, 31638, 6934, 32611, 7194, 36133, 7025, 
30138, 6884, 26935, 6900, 20333, 8021, 18929, 5619, 11157, 4332, 
10778, 4437, 7974, 3416, 9270, 3793), Mean = c(212.811764705882, 
73.1041666666667, 215.032608695652, 73.0566037735849, 213.363636363636, 
76.3818181818182, 211.482142857143, 80.7460317460317, 230.612068965517, 
81.9130434782609, 223.793650793651, 85.4342105263158, 243.369230769231, 
88.8974358974359, 250.853846153846, 89.925, 280.100775193798, 
84.6385542168675, 241.104, 84.9876543209877, 228.262711864407, 
89.6103896103896, 209.618556701031, 111.402777777778, 378.58, 
137.048780487805, 265.642857142857, 127.411764705882, 256.619047619048, 
134.454545454545, 306.692307692308, 170.8, 319.655172413793, 
130.793103448276), sd = c(365.391684625249, 97.8420871855394, 
363.635291602196, 94.1282707255493, 366.5841066326, 93.8500559223754, 
373.650556559185, 106.929577104772, 405.688052605677, 109.41727188241, 
421.942750950132, 110.801123403007, 462.951922738037, 115.87931358968, 
502.602700547356, 117.741378786224, 642.043293966629, 114.535815924939, 
459.068496259615, 114.82317860815, 360.246791665663, 119.967995276389, 
293.702281347504, 224.695704072853, 460.551137890511, 162.282154166672, 
231.68690199813, 139.634830604701, 226.30617641151, 143.124310240498, 
343.058102277823, 181.389548819806, 410.53721563181, 192.111645239046
), Variance = c(133511.083193277, 9573.0740248227, 132230.625298614, 
8860.13134978229, 134383.907235622, 8807.832996633, 139614.738416988, 
11433.9344598054, 164582.796026986, 11972.1393861893, 178035.685079365, 
12276.8889473684, 214324.482766846, 13428.0153180153, 252609.474597496, 
13863.032278481, 412219.591327519, 13118.4531295915, 210743.884258065, 
13184.362345679, 129777.750905403, 14392.3198906357, 86261.0300687285, 
50488.159428795, 212107.350612245, 26335.4975609756, 53678.8205574913, 
19497.8859180036, 51214.4854819977, 20484.5681818182, 117688.861538462, 
32902.1684210526, 168540.805418719, 36906.8842364532), Median = c(110, 
34.5, 111.5, 40, 109, 40, 112, 47, 109.5, 34, 111.5, 35, 120.5, 
41.5, 124.5, 46.5, 125, 44, 124, 44, 121, 44, 112, 42.5, 251, 
85, 199.5, 93.5, 186, 88, 206, 111, 185, 50), Min = c(5, 1, 2, 
1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 
19, 1, 19, 1, 19, 1, 49, 5, 0, 0), Max = c(2771, 414, 2827, 408, 
2994, 390, 3196, 506, 3421, 524, 3680, 553, 3952, 570, 4271, 
581, 4585, 601, 3898, 602, 2603, 592, 1951, 1699, 2269, 675, 
1151, 576, 1122, 565, 1680, 652, 2086, 809)), .Names = c("Year", 
"variable", "N", "Sum", "Mean", "sd", "Variance", "Median", "Min", 
"Max"), row.names = c(NA, -34L), class = "data.frame")

I made a plot:

theplot<- ggplot(subset(hej3,variable=="Delägare.män."), aes(x = Year)) + 

geom_line(aes(y = Mean, color = "Medelvärde"), 
size = 1.5, linetype = 1, alpha = 1) + 

geom_ribbon(aes(ymax = Max, 
ymin = Min, fill = "Delägare Män Max/Min"), linetype = 3, 
alpha = 0.4) + 

geom_ribbon(aes(ymax = Mean+sd, ymin = Mean-sd, fill = "Mean +- sd"), 
colour = "grey50", linetype = 3, alpha = 0.8)+

#geom_line(aes(y = Sum, 
#color = "Sum Delägare Män"), size = 0.9, linetype = 1, alpha = 1) + 

geom_line(aes(y = N, 
                color = "Antal Kassor"), size = 0.9, linetype = 2, alpha = 1)+

scale_y_continuous(breaks = seq(-500, 4800, by = 100), limits = c(-500, 4800), 
                   labels = seq(-500, 4800, by = 100))+ 
  scale_x_continuous(breaks=seq(1901,1930,2))+
labs(title = "Manliga Delägare i Yrkeskassor") +

  scale_color_manual("Variabler", breaks = c("Antal Kassor","Medelvärde"),  
                     values = c("Antal Kassor" = "black", "Medelvärde" = "#6E6E6E")) + 
 scale_fill_manual(" Ribbons", breaks = c("Delägare Män Max/Min", "Mean +- sd"), 
                    values = c(`Delägare Män Max/Min` = "grey50", `Mean +- sd` = "#4E4E4E")) + 
  theme(legend.direction = "horizontal", legend.position = "bottom", legend.key = element_blank(), 
        legend.background = element_rect(fill = "white", colour = "gray30")) + 
  guides(fill = guide_legend(keywidth = 0.9, keyheight = 1)) 

I have 2 questions:

  1. How can I change the line type in the legend? I would like the "Antal Kassor variable in the legend to be dashed as in the picture (linetype=2).

  2. How can I "zoom in" a part of y-axis? I would like to zoom in the area between -300 och 600 at the y-axis.

I know there are a lot of ggplot wizards here:) Best Regards!

解决方案

To use your original data frame you should change to lines. In both calls to geom_line() put linetype= inside the aes() and set the type to variable name.

 + geom_line(aes(y = Mean, color = "Medelvärde",linetype = "Medelvärde"), 
            size = 1.5, alpha = 1)  
 + geom_line(aes(y = N, 
                color = "Antal Kassor",linetype="Antal Kassor"), size = 0.9, alpha = 1)

Then you should add scale_linetype_manual() with the same name as for scale_colour_manual() and there set line types you need.

+scale_linetype_manual("Variabler",values=c("Antal Kassor"=2,"Medelvärde"=1))

Also guides() should be adjusted for linetype and colours to better show lines in legend.

+ guides(fill = guide_legend(keywidth = 1, keyheight = 1),
    linetype=guide_legend(keywidth = 3, keyheight = 1),
    colour=guide_legend(keywidth = 3, keyheight = 1))

Here is complete code used:

theplot<- ggplot(subset(hej3,variable=="Delägare.män."), aes(x = Year)) + 

  geom_line(aes(y = Mean, color = "Medelvärde",linetype = "Medelvärde"), 
            size = 1.5, alpha = 1) + 

  geom_ribbon(aes(ymax = Max, 
                  ymin = Min, fill = "Delägare Män Max/Min"), linetype = 3, 
              alpha = 0.4) + 

  geom_ribbon(aes(ymax = Mean+sd, ymin = Mean-sd, fill = "Mean +- sd"), 
              colour = "grey50", linetype = 3, alpha = 0.8)+

  #geom_line(aes(y = Sum, 
  #color = "Sum Delägare Män"), size = 0.9, linetype = 1, alpha = 1) + 

  geom_line(aes(y = N, 
                color = "Antal Kassor",linetype="Antal Kassor"), size = 0.9, alpha = 1)+

  scale_y_continuous(breaks = seq(-500, 4800, by = 100), limits = c(-500, 4800), 
                     labels = seq(-500, 4800, by = 100))+ 
  scale_x_continuous(breaks=seq(1901,1930,2))+
  labs(title = "Manliga Delägare i Yrkeskassor") +

  scale_color_manual("Variabler", breaks = c("Antal Kassor","Medelvärde"),  
                     values = c("Antal Kassor" = "black", "Medelvärde" = "#6E6E6E")) + 
  scale_fill_manual(" Ribbons", breaks = c("Delägare Män Max/Min", "Mean +- sd"), 
                    values = c(`Delägare Män Max/Min` = "grey50", `Mean +- sd` = "#4E4E4E")) + 
  scale_linetype_manual("Variabler",values=c("Antal Kassor"=2,"Medelvärde"=1))+
  theme(legend.direction = "horizontal", legend.position = "bottom", legend.key = element_blank(), 
        legend.background = element_rect(fill = "white", colour = "gray30")) + 
  guides(fill = guide_legend(keywidth = 1, keyheight = 1), linetype=guide_legend(keywidth = 3, keyheight = 1),
         colour=guide_legend(keywidth = 3, keyheight = 1)) +
     coord_cartesian(ylim = c(-300, 600))

这篇关于更改ggplot图例中的线型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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