点图又叫Cleveland dot plot,克利夫兰点图。可以在水平线上绘制大量的点,更好的表示点之间的关系。强调数据的排序展示以及数据之间的差距。点图一般是横向展示,所以y轴为类别型变量,x轴为需要展示的数据值。
dotchart(x, labels = NULL, groups = NULL, gdata = NULL,
ann = par("ann"), xaxt = par("xaxt"), frame.plot = TRUE, log = "",
cex = par("cex"), pt.cex = cex,
pch = 21, gpch = 21, bg = par("bg"),
color = par("fg"), gcolor = par("fg"), lcolor = "gray",
xlim = range(x[is.finite(x)]),
main = NULL, xlab = NULL, ylab = NULL, ...)
R语言的base包里面自带的dotchart函数可以绘制点图。
上面简介中的例子copy自dotchart的函数帮助文档。
dotchart(VADeaths, main = "Death Rates in Virginia - 1940")
数据是1940年每1000人的死亡率。数据按照年龄段来区分,并且按照地方,男女性分组。
> VADeaths
Rural Male Rural Female Urban Male Urban Female
50-54 11.7 8.7 15.4 8.4
55-59 18.1 11.7 24.3 13.6
60-64 26.9 20.3 37.0 19.3
65-69 41.0 30.9 54.6 35.1
70-74 66.0 54.3 71.1 50.0
我想比较特定地方的男女死亡率的比较。这样的话,数据需要处理。下面的code处理的比较麻烦。但是达到了我要的效果。
ruralDFMale <- data.frame(rownames(VADeaths),VADeaths[,1], c("Male","Male","Male","Male","Male"))
names(ruralDFMale ) <- c("AgeGroup", "DeathRate","Gender" )
ruralDFFeMale <- data.frame(rownames(VADeaths),VADeaths[,2], c("Female","Female","Female","Female","Female"))
names(ruralDFFeMale) <- c("AgeGroup", "DeathRate","Gender" )
ruralDF <- rbind(ruralDFMale, ruralDFFeMale)
colorFun <- function(gender) {
colorVec <- vector()
for(g in gender) {
if(g == "Female") {
colorVec <- c(colorVec, "Red")
} else {
colorVec <- c(colorVec, "Blue")
}
}
colorVec
}
dotchart(ruralDF$DeathRate, main = "Death Rates in Virginia - 1940", groups=ruralDF$AgeGroup,color=colorFun(ruralDF$Gender))
处理过后的数据
> ruralDF
AgeGroup DeathRate Gender
50-54 50-54 11.7 Male
55-59 55-59 18.1 Male
60-64 60-64 26.9 Male
65-69 65-69 41.0 Male
70-74 70-74 66.0 Male
50-541 50-54 8.7 Female
55-591 55-59 11.7 Female
60-641 60-64 20.3 Female
65-691 65-69 30.9 Female
70-741 70-74 54.3 Female