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?
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With