haimen
haimen

Reputation: 2015

Modifying the legend colour and text in ggplot - R

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

Upvotes: 1

Views: 3164

Answers (1)

aosmith
aosmith

Reputation: 36076

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

Upvotes: 3

Related Questions