1

My data set contains three columns, name, value and indicator. I want to plot a bar graph with these three variables. the following is my code,

ggplot(data , aes(x = factor(name), y = value, color = as.factor(indicator))) + geom_bar(stat = "identity" ) + 
    scale_color_manual(values = c("lightblue", "red"), guide = guide_legend(title = "text", labels = c('A', 'B'))) +  
    xlab("xlab") + ylab("ylab") + ggtitle("title") 

and following is the graph I got,

enter image description here

But the legend here is not showing the lightblue and red. Only the outline is of that colour, but inside it shows only grey. Can anybody help me in filling the legend with the similar color as the graph color. Also I want to change the 0,1 to A and B text.

Can anybody help me in doing this? Any help would be appreciated,

Thanks

haimen
  • 1,985
  • 7
  • 30
  • 53
  • Perhaps, [this](http://stackoverflow.com/questions/30962946/remove-grey-from-legend-in-ggplot2) might help and [this](http://stackoverflow.com/questions/31198113/how-to-change-legend-label-in-theme-argument-in-ggplot2) for changing labels – Miha Jul 18 '16 at 21:21
  • @Miha Both are not working for me. The code runs, but there is no change in the graph. Can you help me in modifying the code? – haimen Jul 18 '16 at 21:31
  • 2
    I think you want to fill the bars with color, not just color the outlines so try `fill` instead of `color`. Then change the `name` and `labels` along with `values` using `scale_fill_manual`. – aosmith Jul 18 '16 at 21:56
  • @aosmith it works. Thank you. Do you want to put this as an answer? – haimen Jul 18 '16 at 22:09

1 Answers1

3

The color aesthetic only changes the outline color of bars in ggplot2. To change the color of the entire bar you'll want to use fill instead.

After replacing color with fill in your code, you can change the colors used along with the title and labels of the legend in scale_fill_manual.

scale_fill_manual(values = c("lightblue", "red"), name = "text", labels = c('A', 'B'))
aosmith
  • 34,856
  • 9
  • 84
  • 118