Legend with different symbol sizes in base R
12,240
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).
Author by
Karsten W.
Updated on June 09, 2022Comments
-
Karsten W. almost 2 years
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?
-
IRTFM over 12 yearsSomebody could to the R world a big favor and document all the "cex" variations in one central location.