如何在ggplot中添加图例(不显示) [英] How to add a legend in ggplot (not showing up)

查看:299
本文介绍了如何在ggplot中添加图例(不显示)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个图例,其中红线=拒绝率,深蓝色=平均值(reject_rate)
,浅蓝色=平均值+2标准差

我已将线条绘制得很好,但似乎无法显示图例。非常感谢



>

数据

 > dput(sub)
structure(list(Report_Date = structure(c(16311,16318,16325,$ b $ 16332,16339,16346,16353,166360,16367,16374,16381,16388,
16311,16318,16325,16332,16339,16346,16360,16367,16374,
16381,16388,16311,16318,16325,16332,16339,16346,16353,$ b $ 16360,16367,16374, 16381,16388,16311,16318,16325,16332,
16339,16346,16360,16367,16374,16388,16311,16318,16325,
16332,16339,16346,16353,16360,16367, 16374,16381,16388,
16311,16318,16325,16332,16339,16346,16353,16360,16367,
16374,16381,16388,16311,16318,16325,16332,16339,16346,16344,16384,16384,16384,16384,16384,16384,
16353,166360,16367,16374,16381,16388),class =Date),Regional_Office = structure(c(1L,
1L,1L,1L,1L,1L,1L,1L, 1L,1L,1L,1L,2L,2L,2L,2L,2L,
2L,2L,2L,2L,2L,2L,3L,3L,3L,3L,3L,3L,3L,3L, 3L,3L,
3L,3L,4L,4L,4L,4L,4L,4L,4L,4L,4L,4L,5L,5L,5L,5L,
5L,5L, 5L,5L,5L,5L,5L,6L,6L,6L,6L,6L,6L, 6L,6L,6L,6L,6L,7L,7L,7L,7L,7L,7L,7L,7L,7L,7L,7L,7L,7L
),。标签= c EARO, EASO, LACRO, MEERO, SAPO, SARO,
的WARO),类= 因子),已批准= C(0.973856209150327,0.917159763313609,
0.913978494623656,0.930612244897959,0.902857142857143,0.952380952380952
1,0.979763912310287 0.950873362445415 0.802325581395349 0.950354609929078
1,0.976284584980237 0.99625 1,0.962962962962963 0.958837772397094 0.958837772397094
1,1,1, 1,0.978723404255319,1,0.993650793650794,0.994897959183674,
1,0.995316159250585,0.966911764705882,1,1,0.991836734693878,
0.995180722891566,0.999535714285714,0.9989010989010989,1.1,
1,1,1, 0.897435897435897,1,0.985507246376812,0.907692307692308,
0.952380952380952,1,0.982558139534884,0.989247311827957,1,
0.985383678440926,0.972555746140652,0.978571428571429,1,0.973352033660589,
0.934504792332268,0.973421926910299,0.985074626865672,1,0.9562043 79562044,
0.901060070671378,0.9981196581196581,0.96875,0.897560975609756,
0.988505747126437,1,0.95,0.895384615384615,0.916666666666667,
0.967741935483871,0.935483870967742,1,1,1,0.948275862068966,
0.994845360824742,1,0.970833333333333,1,0.936781609195402,
0.987854251012146,0.982758620689655),approval_rate = C(97.3856209150327,
91.7159763313609,91.3978494623656,93.0612244897959,90.2857142857143,
95.2380952380952,100,97.9763912310287,95.0873362445415,80.2325581395349
95.0354609929078,100,97.6284584980237,99.625,100,96.2962962962963
95.8837772397094,100,100,100,100,97.8723404255319,100
99.3650793650794 99.4897959183673,100 99.5316159250585 96.6911764705882
100,100,99.1836734693878,99.5180722891566,99.5535714285714
98.9010989010989,100,100,100,100,100,79.7435897435898
100 98.5507246376812 90.7692307692308 95.2380952380952 100
98.2558139534884 ,9 8.9247311827957,100,98.5383678440926,97.2555746140652,
97.8571428571428,100,97.3352033660589,93.4504792332268,97.3421926910299,
98.5074626865672,100,95.6204379562044,90.1060070671378,98.1196581196581,
96.875,89.7560975609756,98.8505747126437,100,95, 89.5384615384615,
91.6666666666667,96.7741935483871,93.5483870967742,100,100,
100,100,94.8275862068966,999.4845360824742,100,97.0833333333333,
100,93.6781609195402,98.7854251012146,98.2758620689655),reject_rate = c(2.61437908496732 ,
8.28402366863905 8.60215053763441 6.93877551020409 9.71428571428572
4.76190476190477,0.22360876897133 4.91266375545851 19.7674418604651
4.96453900709219,0.237154150197628 0.375000000000003,0
3.70370370370371 4.11622276029056 0 ,0,0,0,2,12765957446809,
0,0.634920634920633,0.510204081632648,0,0.468384074941453,
3.30882352941176,0,0,0.816326530612244,0.481927710843377,
0.44642857142 857,1.09890109890109,0,0,0,0,0,10.2564102564103,
0,1.44927536231884,9.23076923076923,4.76190476190477,0.1.74418604651163,
1.0752688172043,0,1.46163215590743,2.74442538593482,2.14285714285715,
0,2.66479663394109,6.54952076677317,2.65780730897009,1.49253731343284,
0,4.37956204379562,9.89399293286219,1.88034188034188,3.125,
10.2439024390244,1.14942528735632,0.5,10.4615384615385,8.33333333333334,
3.2258064516129,6.45161290322581,0, 0,0,0,5.17241379310345,
0.515463917525771,0,2.91666666666667,0,6.32183908045977,
1.21457489878543,1.72413793103449)),.Names = c(Report_Date,
Regional_Office, (5L,6L,7L,8L,9L,10L,11L,12L,13L,14L,
15L,16L, 21L,22L,23L,24L,25L,26L,27L,28L,29L,30L,31L,
36L,37L,38L,39L,40L,41L,42L,43L,44L,45L,46L,47L, 51L,
52L,53L,54L,55L,56L,57L,58L,59L,60L,65L,66L, 67L,68L,
69L,70L,71L,72L,73L,74L,75L,76L,81L,82L,83L,84L,85L,$ b $ 86L,87L,88L,89L,90L,91L, 92L,97L,98L,99L,100L,101L,
102L,103L,104L,105L,106L,107L,108L),class =data.frame)


代码

regional_trend< - 功能(子,n_weeks,COLS){
ggplot(子,AES(REPORT_DATE,reject_rate))+
geom_point(颜色= 红)+
facet_wrap(〜Regional_Office ,ncol = cols)+
geom_line(color =red,size = 1.5)+
geom_text(aes(y = reject_rate,label = round(reject_rate,1)),vjust = -1, (c(min,sub $ Report_Date),max(sub $ Report_Date)))+
ylim(c(0,max(sub $ reject_rate,size = 4.5,color =red)+
xlim +)+
ggtitle(粘贴(按区域拒绝率\ n(滚动,n周,周)))+
主题(plot.title = element_text(size = 15) ,color =bold,vjust = 2),
axis.title.x = element_text(color =grey20,size = 14,
angle = 0,hjust = .5,vjust = 0,face =plain),
axis.title。 y = element_text(color =grey20,size = 14,
angle = 90,hjust = .5,vjust = 1,face =plain)
)+
xlab )+
ylab(拒绝率(%))+
geom_hline(yintercept =平均值(sub $ reject_rate),线型=虚线,颜色=蓝色,尺寸= .7)+
geom_hline(yintercept = mean(sub $ reject_rate)+(sd(sub $ reject_rate)* 2),
linetype =longdash,color =lightblue3,size =。 7)
}

regional_trend(reg_last_12wks,12,1)

解决方案

通常情况下,您应该在美学调用中使用颜色,以便自动工作。你也可以用黑客来实现它:在你的情节中添加一个额外的 geom_line 。这通常不是一个好主意,但它应该起作用。

  p + geom_line(data = data.frame(x = as.Date(2014-08-29),y = -Inf,
col = c('拒绝率','平均拒绝率','平均值+2sd')),
aes(x,y,color = col))+
scale_color_manual('',
limits = c('拒绝率','平均拒绝率','mean + 2sd'),
values = c('red','blue','lightblue3 '))


I'd like to add a legend where the red line = reject rate, dark blue = mean(reject_rate) and light blue = mean + 2 standard deviations

I've got the lines plotted fine, but can't seem to get a legend to show up. Many thanks

data

> dput(sub)
structure(list(Report_Date = structure(c(16311, 16318, 16325, 
16332, 16339, 16346, 16353, 16360, 16367, 16374, 16381, 16388, 
16311, 16318, 16325, 16332, 16339, 16346, 16360, 16367, 16374, 
16381, 16388, 16311, 16318, 16325, 16332, 16339, 16346, 16353, 
16360, 16367, 16374, 16381, 16388, 16311, 16318, 16325, 16332, 
16339, 16346, 16360, 16367, 16374, 16388, 16311, 16318, 16325, 
16332, 16339, 16346, 16353, 16360, 16367, 16374, 16381, 16388, 
16311, 16318, 16325, 16332, 16339, 16346, 16353, 16360, 16367, 
16374, 16381, 16388, 16311, 16318, 16325, 16332, 16339, 16346, 
16353, 16360, 16367, 16374, 16381, 16388), class = "Date"), Regional_Office = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L
), .Label = c("EARO", "EASO", "LACRO", "MEERO", "SAPO", "SARO", 
"WARO"), class = "factor"), Approved = c(0.973856209150327, 0.917159763313609, 
0.913978494623656, 0.930612244897959, 0.902857142857143, 0.952380952380952, 
1, 0.979763912310287, 0.950873362445415, 0.802325581395349, 0.950354609929078, 
1, 0.976284584980237, 0.99625, 1, 0.962962962962963, 0.958837772397094, 
1, 1, 1, 1, 0.978723404255319, 1, 0.993650793650794, 0.994897959183674, 
1, 0.995316159250585, 0.966911764705882, 1, 1, 0.991836734693878, 
0.995180722891566, 0.995535714285714, 0.989010989010989, 1, 1, 
1, 1, 1, 0.897435897435897, 1, 0.985507246376812, 0.907692307692308, 
0.952380952380952, 1, 0.982558139534884, 0.989247311827957, 1, 
0.985383678440926, 0.972555746140652, 0.978571428571429, 1, 0.973352033660589, 
0.934504792332268, 0.973421926910299, 0.985074626865672, 1, 0.956204379562044, 
0.901060070671378, 0.981196581196581, 0.96875, 0.897560975609756, 
0.988505747126437, 1, 0.95, 0.895384615384615, 0.916666666666667, 
0.967741935483871, 0.935483870967742, 1, 1, 1, 1, 0.948275862068966, 
0.994845360824742, 1, 0.970833333333333, 1, 0.936781609195402, 
0.987854251012146, 0.982758620689655), approval_rate = c(97.3856209150327, 
91.7159763313609, 91.3978494623656, 93.0612244897959, 90.2857142857143, 
95.2380952380952, 100, 97.9763912310287, 95.0873362445415, 80.2325581395349, 
95.0354609929078, 100, 97.6284584980237, 99.625, 100, 96.2962962962963, 
95.8837772397094, 100, 100, 100, 100, 97.8723404255319, 100, 
99.3650793650794, 99.4897959183673, 100, 99.5316159250585, 96.6911764705882, 
100, 100, 99.1836734693878, 99.5180722891566, 99.5535714285714, 
98.9010989010989, 100, 100, 100, 100, 100, 89.7435897435898, 
100, 98.5507246376812, 90.7692307692308, 95.2380952380952, 100, 
98.2558139534884, 98.9247311827957, 100, 98.5383678440926, 97.2555746140652, 
97.8571428571428, 100, 97.3352033660589, 93.4504792332268, 97.3421926910299, 
98.5074626865672, 100, 95.6204379562044, 90.1060070671378, 98.1196581196581, 
96.875, 89.7560975609756, 98.8505747126437, 100, 95, 89.5384615384615, 
91.6666666666667, 96.7741935483871, 93.5483870967742, 100, 100, 
100, 100, 94.8275862068966, 99.4845360824742, 100, 97.0833333333333, 
100, 93.6781609195402, 98.7854251012146, 98.2758620689655), reject_rate = c(2.61437908496732, 
8.28402366863905, 8.60215053763441, 6.93877551020409, 9.71428571428572, 
4.76190476190477, 0, 2.02360876897133, 4.91266375545851, 19.7674418604651, 
4.96453900709219, 0, 2.37154150197628, 0.375000000000003, 0, 
3.70370370370371, 4.11622276029056, 0, 0, 0, 0, 2.12765957446809, 
0, 0.634920634920633, 0.510204081632648, 0, 0.468384074941453, 
3.30882352941176, 0, 0, 0.816326530612244, 0.481927710843377, 
0.44642857142857, 1.09890109890109, 0, 0, 0, 0, 0, 10.2564102564103, 
0, 1.44927536231884, 9.23076923076923, 4.76190476190477, 0, 1.74418604651163, 
1.0752688172043, 0, 1.46163215590743, 2.74442538593482, 2.14285714285715, 
0, 2.66479663394109, 6.54952076677317, 2.65780730897009, 1.49253731343284, 
0, 4.37956204379562, 9.89399293286219, 1.88034188034188, 3.125, 
10.2439024390244, 1.14942528735632, 0, 5, 10.4615384615385, 8.33333333333334, 
3.2258064516129, 6.45161290322581, 0, 0, 0, 0, 5.17241379310345, 
0.515463917525771, 0, 2.91666666666667, 0, 6.32183908045977, 
1.21457489878543, 1.72413793103449)), .Names = c("Report_Date", 
"Regional_Office", "Approved", "approval_rate", "reject_rate"
), row.names = c(5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 
36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 51L, 
52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 65L, 66L, 67L, 68L, 
69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 81L, 82L, 83L, 84L, 85L, 
86L, 87L, 88L, 89L, 90L, 91L, 92L, 97L, 98L, 99L, 100L, 101L, 
102L, 103L, 104L, 105L, 106L, 107L, 108L), class = "data.frame")

code

regional_trend <- function(sub,n_weeks,cols) {
  ggplot(sub, aes(Report_Date, reject_rate))+
    geom_point(color="red") +
    facet_wrap(~Regional_Office, ncol=cols) +
    geom_line(color="red", size=1.5) +
    geom_text(aes(y=reject_rate, label=round(reject_rate,1)), vjust=-1, size=4.5,colour="red") +
    xlim(c(min(sub$Report_Date),max(sub$Report_Date))) +
    ylim(c(0,max(sub$reject_rate+15)))+
    ggtitle(paste("Reject Rates by Region \n(Rolling",n_weeks, "weeks)")) +
    theme(plot.title = element_text(size=15, face="bold", vjust=2),
          axis.text.x = element_text(colour="grey20",size=10,,hjust=.5,vjust=.5),
          axis.title.x = element_text(colour="grey20", size=14, 
           angle=0,hjust=.5, vjust=0, face="plain"),
          axis.title.y = element_text(colour="grey20", size=14, 
          angle=90, hjust=.5, vjust=1,face="plain")
    )+
    xlab("Week of Receipt") +
    ylab("Reject Rate (in %)") + 
    geom_hline(yintercept=mean(sub$reject_rate), linetype="dashed",colour="blue",size=.7) +
    geom_hline(yintercept=mean(sub$reject_rate)+ (sd(sub$reject_rate)*2),  
    linetype="longdash", colour="lightblue3", size=.7) 
}

regional_trend(reg_last_12wks,12,1)

解决方案

Usually, you should have the color within the aesthetics call, for this to work automatically. You can also achieve it with a hack: adding an additional geom_line to your plot. This is normally not a good idea, but it should work.

p + geom_line(data=data.frame(x = as.Date("2014-08-29"), y = -Inf, 
                              col=c('rejection rate', 'mean rejection rate', 'mean + 2sd')), 
              aes(x, y, color=col)) + 
  scale_color_manual('',
                     limits = c('rejection rate', 'mean rejection rate', 'mean + 2sd'), 
                     values = c('red', 'blue', 'lightblue3')) 

这篇关于如何在ggplot中添加图例(不显示)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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