Reputation: 3941
I'm making a bubble plot using 'symbols', and I'd like to have a legend outside of the plot area showing an index of bubble sizes (similar to what ggplot would produce). I'd like to stick with symbols and avoid ggplot.
My Data:
V1<-c(1,10,30,22,22,20)
V2<-c(4,17,4,33,33.2,15)
V3<-c(20,20,15,34,33,30)
V4<-c("A","A","A","B","B","B")
DF<-data.frame(V1,V2,V3,V4)
My plot without a legend:
symbols(DF$V1,DF$V2,circles=V3,inches=0.35,fg="darkblue",bg="red")
text(DF$V1,DF$V2+c( 0, 0, 0, 1, -1,0 ),V4,cex=0.5)
And here is an example using ggplot. I am hoping to create a legend similar to the one in this plot, and use it with the above symbols code.
library(ggplot2)
ggplot(DF,aes(x=V1,y=V2,size=V3,label=V4),legend=FALSE) +
geom_point(color='darkblue',
fill="red", shape=21) +
theme_bw() +
geom_text(size=5)
Upvotes: 2
Views: 2837
Reputation: 15461
something like:
symbols(DF$V1,DF$V2, circles = V3, inches = 0.35, fg = "darkblue", bg = "red")
text(DF$V1, DF$V2 + c( 0, 0, 0, 1, -1,0 ), V4, cex = 0.5)
legend(
"topright",
legend=c("15", "20", "25", "30"),
pch = 21,
bty = "n",
col = "black",
pt.bg = "red",
pt.cex = c(0.5,1,1.5,2)
)
# thanks to at @Josh O'Brien for pt.cex
Upvotes: 2