这是使用基本图形和my.symbols
TeachingDemos 包中的函数的一种方法:
library(TeachingDemos)
ms.Cleveland <- function(num.pt = 1, cex=2, ...) {
funs <- list(
open = function(cex, ...)
points(0,0, pch=1, lwd=2, cex=cex, ...),
filled = function(cex, ...)
points(0,0, pch=16, cex=cex, ...),
half = function(cex, ...)
points(0,0, pch=1, lwd=5, cex=cex, ...),
vert = function(cex, ...) {
points(0,0, pch=1, lwd=2, cex=cex, ...)
points(0,0, pch='|', lwd=2, cex=cex/2, ...)
},
dot = function(....) {
points(0,0, pch=1, lwd=2, cex=cex, ...)
points(0,0, pch=16, cex=cex/3, ...)
}
)
funs[[num.pt]](cex, ...)
}
### create size variable for mtcars
sz <- findInterval( mtcars$wt, quantile( mtcars$wt, c(0.2, 0.4, 0.6, 0.8) ) ) + 1
with(mtcars, my.symbols(wt, mpg, ms.Cleveland, num.pt=sz, add=FALSE,
symb.plots=TRUE))
tmp <- legend('topright', pch=1, col=NA, pt.cex=2,
legend=c('Small', 'SMed', 'Medium', 'LMed', 'Large'))
my.symbols( (tmp$rect$left + tmp$text$x)/2, tmp$text$y, ms.Cleveland, num.pt=1:5,
symb.plots=TRUE)
该ms.Cleveland
函数根据数字(1 到 5)或名称绘制点,并对最后 2 个点使用重叠绘图(可能需要稍微调整函数)。
然后使用常规图例功能绘制图例,但col=NA
符号有位置,但未绘制。然后my.symbols
再次用于将符号放置在图例中框左边缘和文本开头之间的中间点。