2014-11-20 3 views
-1

Я хотел бы добавить легенду, где красная линия = процент брака, темно-синий = средний (reject_rate) и светло-голубой = среднее + 2 стандартных отклоненияКак добавить легенду в ggplot (не отображается)

У меня есть линии, построенные отлично, но, похоже, не может появиться легенда. Большое спасибо

enter image description here

данных

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

код

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) 

enter image description here

+0

Это может помочь http://stackoverflow.com/questions/10349206/add-legend-to-ggplot2-line-plot – KFB

ответ

0

Обычно, у ou должен иметь цвет в вызове эстетики, чтобы это работало автоматически. Вы также можете добиться этого с помощью взлома: добавив дополнительный geom_line к вашему сюжету. Обычно это не очень хорошая идея, но она должна работать.

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