[R] How to add a legend with symbols()

Peter Maclean pmaclean2011 at yahoo.com
Fri Aug 29 03:48:55 CEST 2014


 #How to add a legend for this bubble plot 
#I prefer p2 with a legend for each bubble 
library(graphics) 
set.seed(1234) 
n=20 
x <- rnorm(n)*2 
y <- rnorm(n) * 1/10 
Z <- rnorm(n)+ 100 
df$r <- sqrt(z/pi) 
df$l <- replicate(n, paste(sample(LETTERS, 3, replace=TRUE), collapse="")) 
 
N <- nrow(df) 
p1 <- with(df, { 
op <- palette(rainbow(N, end = 0.9)) 
symbols(x, y, circles = r, inches = 0.2, bg = 1:N, fg = "blue", main = "") 
palette(op) 
text(x, y, l, cex=0.8) 
abline(v=0, h=0)
}) 
 
p2 <- with(df, { 
op <- palette(rainbow(N, end = 0.9)) 
symbols(x, y, circles = r, inches = 0.2, bg = 1:N, fg = "blue", main = "") 
palette(op) 
abline(v=0, h=0) }) 
 

Peter Maclean
Department of Economics
UDSM



More information about the R-help mailing list