0

In ggplot2 legends are tied to aesthetics to my knowledge and I believe that may be my problem here, but I cannot figure out the solution as to why my legend isn't showing. I've read other posts made adjustments, but still cannot get them to show.

Any suggestions are appreciated.

rawAuto data below

LoanType Excessive Income       LoanStatus
 1:       VL         0      0        WITHDRAWN
 2:       VL         0      0        WITHDRAWN
 3:       VL         0      0        WITHDRAWN
 4:       VL         0      0        WITHDRAWN
 5:       VL         1      0         DECLINED
 6:       VL         0      0        WITHDRAWN
 7:       VL         1      0         DECLINED
 8:       VL         0      0        WITHDRAWN
 9:       VL         0      0 INSTANT APPROVED
10:       VL         0      0        WITHDRAWN
11:       VL         0      0 INSTANT APPROVED
12:       VL         0      0     PRE-APPROVED
13:       VL         0      0        WITHDRAWN
14:       VL         0      0 INSTANT APPROVED
15:       VL         0      0 INSTANT APPROVED
16:       VL         0      0 INSTANT APPROVED
17:       VL         0      0     PRE-APPROVED
18:       VL         0      0 INSTANT APPROVED
19:       VL         1      0         DECLINED
20:       VL         0      0        WITHDRAWN

ggplot(rawAuto, aes(LoanStatus)) +
     stat_count(aes(LoanStatus, y = ..count..), show.legend = T, 
     geom = "bar", color = "grey") +
     stat_count(data = rawAuto[Excessive == 0 & Income == 0], aes(LoanStatus, y = ..count..),
     geom = "point", color = "red", show.legend = T) +
     scale_size_continuous(range = c(4, 8)) +
     scale_y_continuous(labels = comma, breaks = 
          c(0,600,1200,1800,2400,3000,3600,4200,4800,5400,6000)) +
     labs(title = "Total auto applications vs. excluding 'Excessive' and 
     'Insufficient Income' denial reasons", x = "Loan Status",
          y = "Auto loan Counts") +
     theme(axis.text.y = element_text(size = 15)) +
     theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
     scale_colour_manual(name = 'Legend', values = c("grey"= "grey",
     "red"= "red"), labels = c('All auto','Subset auto'))
user3067851
  • 524
  • 1
  • 6
  • 20
  • 2
    you don't have `colour` etc in any of the `aes`thetics.. What do you want your legend to show? – Richard Telford Jun 20 '16 at 20:49
  • I want to show a legend for the first stat_count where geom = bar and the other stat_count where geom = point, basically the counts for each dataset. – user3067851 Jun 20 '16 at 20:57
  • An easier approach will likely to be to summarize your data first, creating a variable that distinguishes between the groups and then plot that data. – joran Jun 20 '16 at 21:16
  • just move the `color=` bits into the `aes()` calls. but Joran is right, this wld be cleaner if you summarized first. – hrbrmstr Jun 20 '16 at 21:17
  • point taken! your suggestion did work though. – user3067851 Jun 20 '16 at 21:33

0 Answers0