Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Designating bubble color as a variable is limiting choice in ggplot

Tags:

r

legend

ggplot2

When creating a bubble plot in ggplot, I'm having an issue adjusting two separate aesthetic choices and I'm sure it has something to do with the way I'm coding my plot. In the below plot, if I assign the color of the bubbles based on a variable like so:

V1<-rnorm(50)
V2<-rnorm(50)
V3<-c(rep("A",10),rep("B",10),rep("C",10),rep("D",10),rep("E",10))
V4<-V2+.1
V5<-c(rep("BF1",9),rep("BF2",11),rep("BF3",8),rep("BF4",12),rep("BF5",10))
DF<-data.frame(V1,V2,V3,V4,V5)

ggplot(DF,aes(x=V1,y=V2,size=V4,label=DF$V3,fill=V5),legend=FALSE)+
scale_y_continuous( limits = c(-3, 3))+
scale_x_continuous( limits = c(-3, 3))+
geom_point(color="black",shape=21,alpha=0.5)+
geom_text(size=2)+
theme_bw()+
scale_size(range = c(5, 20))+
scale_colour_brewer(palette="Blues")

enter image description here

The two issues I have with this plot are

1- I wanted to change the default pallet of colors to something less pastel. I thought using the scale_color_brewer argument would change the colors to "Blues", but obviously this is not the case.

2- Secondly, the legend for V4 now has empty circles, whereas without the aes(fill=) argument the circles were filled. I know this is because I have many colors for my circles now, but I'd prefer to have a solid (black) circle be my legend as opposed to the open circles. Is there a way to override the legend and make the circles filled?

like image 832
Vinterwoo Avatar asked Dec 09 '13 00:12

Vinterwoo


1 Answers

For (1) use scale_fill_brewer(palette="Blues")

For (2) use guides(size=guide_legend(override.aes = list(fill="black", alpha=1)))


update: @BenBoker makes a great point in his comment above. I would suggest at the very least, cranking up the alpha in the legend: add to (2) above: , color=guide_legend(override.aes = list(alpha=0.9)) or something of that nature

like image 74
Ricardo Saporta Avatar answered Sep 28 '22 19:09

Ricardo Saporta