如何在条形图上添加中心线,指示R中条形图上的类中点



我在R中有以下代码:

bloodgroup <- c(1,1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4,4,5,5,5,5)
xx <- barplot(table(bloodgroup),ylim=c(0, 14))
coords <- as.numeric(table(bloodgroup))
text(x = xx, y = coords, label = coords, 
cex = 0.8,pos = 3, col = "red")

我想要在x轴上的1 2 3 4 5以下标签:1-7、8-14、15-21、22-28、29-35。在每个栏上,我希望在中心有一条垂直线,指示类的中点。我该怎么做?

您可以将向量转换为因子,并使用该因子标记x轴。

bloodgroup <- c(1,1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4,4,5,5,5,5)
bloodgroup <- as.data.frame(bloodgroup)
bloodgroup <- within(bloodgroup, {
bloodgroup_factor <- factor(bloodgroup, labels=c('1-7','8-14','15-21',
'22-28','29-35'))
})
xx <- barplot(table(bloodgroup),ylim=c(0, 14))
coords <- as.numeric(table(bloodgroup$bloodgroup_factor))
text(x = xx, y = coords, label = coords, 
cex = 0.8,pos = 3, col = "red")

添加指示中点的中心线:

abline(v=xx)

最新更新