0

I have an issue and can't find the answer here on StackOverflow.

No matter what I do, the legend doesn't want to show up on my plot. I have a simple data frame:

'data.frame':   22 obs. of  8 variables:


$ fan_page_id   : Factor w/ 11 levels "Brand1",..: 10 10 8 8 4 4 3 3 9 9 ...
 $ is_paid       : Factor w/ 2 levels "niesponsorowane",..: 1 2 1 2 1 2 1 2 1 2 ...
 $ all_activity  : num  55 225 45 354 41 ...
 $ reach         : num  13300 13300 24200 24200 62000 62000 52700 52700 11300 11300 ...
 $ color_main    : chr  "#34445b" "#34445b" "#34445b" "#34445b" ...
 $ color_brighter: chr  "#f8bd99" "#f8bd99" "#f8bd99" "#f8bd99" ...
 $ color         : chr  "#34445b" "#ef5a00" "#34445b" "#ef5a00" ...
 $ color2        : chr  "#8B9EBC" "#f8bd99" "#8B9EBC" "#f8bd99" ...

And I want to plot a chart with this code:

  plot = ggplot(data=dane, aes(x=fan_page_id, y=all_activity), fill = all_activity) +
    geom_bar(data = dane, aes(x = fan_page_id, y = (reach)/50, group = dane$is_paid), fill = dane$color2, stat="identity", position = position_dodge(0.9)) +
    geom_bar(data = dane, aes(x = fan_page_id, y = all_activity, group = dane$is_paid), fill = dane$color, stat="identity", position = position_dodge(0.9)) +
    scale_fill_manual(values=c('black','orange'), labels=c(sponsored_label,non_sponsored_label), breaks = c(sponsored_label,non_sponsored_label))+
    geom_text(data=dane, aes(x=fan_page_id, y=(max(reach)*.0011), label=round(all_activity),
                             group = dane$is_paid),
              size=12,
              family='Lato Black',
              color='white',
              hjust = 0.28,
              position = position_dodge(0.9))+
    geom_text(data=dane, aes(x=fan_page_id, y=reach/50, label=round(reach)),
              size=12,
              family='Lato Black',
              color=dane$color_main,
              hjust = -0.2,
              position = position_dodge(0.9))+
    coord_flip()+
    ylim(c(-(max(dane$reach)*.00001), max(dane$reach/50)+0.2*max(dane$reach/50)))+
    myTheme+
    theme(axis.title.x = element_blank(),
          axis.title.y = element_blank(),
          axis.text.x = element_blank(),
          axis.ticks = element_blank(),
          legend.position = "bottom", legend.direction = "horizontal")+
    annotate("text", x = nrow(dane)/2, y = max(dane$all_activity)*0.19, label = 'Test1', hjust = 1, family = "Lato Black", size = 6, colour = so_orange)+
    #annotate("text", x = nrow(dane)+0.13, y = dane_plot$coactive[2], label = coac.plot@labels$middle,  hjust = 0, family = "Lato Black", size = 6, colour = "#000000")+
    annotate("text", x = nrow(dane)/2, y = max(dane$all_activity)*0.5, label = 'Test2',  hjust = 1, family = "Lato Black", size = 6, colour =  '#f8bd99')

  file = ps('activities2.png')
  png(file, 1400, 1000)
  print(plot)
  dev.off()

myTheme also looks fine:

 $ legend.key.size :Class 'unit'  atomic [1:1] 10
  .. ..- attr(*, "valid.unit")= int 7
  .. ..- attr(*, "unit")= chr "mm"
 $ legend.text     :List of 11
  ..$ family       : chr "Lato Light"
  ..$ face         : NULL
  ..$ colour       : chr "black"
  ..$ size         : num 24
  ..$ hjust        : NULL
  ..$ vjust        : NULL
  ..$ angle        : NULL
  ..$ lineheight   : NULL
  ..$ margin       : NULL
  ..$ debug        : NULL
  ..$ inherit.blank: logi FALSE
  ..- attr(*, "class")= chr [1:2] "element_text" "element"
 $ legend.title    : list()
  ..- attr(*, "class")= chr [1:2] "element_blank" "element"
 $ legend.position : chr "right"
 $ legend.direction: chr "vertical"

Well, it's the new ggplot 2 and I have found some issues with scale_fill_manual that I have used previously to get the legend. What can I do in order to have the legend on the plot?

gkmagic
  • 187
  • 1
  • 1
  • 13
  • 1
    I don't know what you want to show but make sure that what has to appear in the legend is inside `aes()`, like the `fill=` if that's relevant. – Haboryme Oct 28 '16 at 10:18
  • Do not (I repeat, *do not*) use `$` within `aes`. ggplot2 looks within the data.frame passed as `data` for the columns specified in `aes`. – Roland Oct 28 '16 at 10:58
  • Furthermore you do not map fill colors to anything (you need to do that inside `aes`, there are numerous examples on Stack Overflow). Without a mapping there is no legend. – Roland Oct 28 '16 at 11:00

0 Answers0