Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Legend with different symbol sizes in base R

Tags:

r

legend

size

In my chart I encode some information in the diameter of the circles plotted. My question is, what is the easiest way to document that information in a legend?

Here is what I tried until now:

dat <- rnorm(100)
cex_brks <- quantile(dat, c(0.25,0.5,0.75))
cex_size <- c(1,1.4,1.8, 2.2) 
cex <- rep(NA, length(dat))
for (i in 1:3) {
    cex[is.na(cex) & dat<=cex_brks[[i]]] <- cex_size[[i]]
}
cex[is.na(cex)] <- cex_size[[4]]
plot(dat, cex=cex, pch=21)
legend(
    "bottom", 
    legend=c("very small", "small", "large", "very large"), 
    bty="n",
    pch=21,
    cex=cex_size
)

However, doing it this way, not only is the symbol (pch) changed in size, but the legend text as well. How can I override this so that only the legend symbols are different sizes?

like image 769
Karsten W. Avatar asked Jan 10 '12 17:01

Karsten W.


1 Answers

You are looking for the pt.cex argument to legend().

cex controls the size of the text in the legend (as well as providing the default values for pt.cex and title.cex, to be used if they are not otherwise specified).

like image 83
Josh O'Brien Avatar answered Oct 08 '22 02:10

Josh O'Brien