首页 > 解决方案 > 如何在 R 中删除重复的 x 轴标签

问题描述

我试图获得按性别和大小间隔( )分组的barplot代表平均着色百分比( )。但是,x 轴上的标签出现重复。我想为每个级别获取一个标签(第一列和第二列一起使用“50-55”,第三列和第四列一起使用“55-60”,依此类推)。我怎么能这样做?valoresclassclass

这是我的代码:

par(mar=c(7,4,4,2)+0.1)
class<-factor(coloration$clase.2,levels=c("50-55","55-60","60-65","65-70","70-75","75-80"))
sex<-factor(coloration$sexo,levels=c("M","H"))
valores<-coloration$perc.greenblue
graf<-barplot(tapply(valores,list(sex,class),mean),beside=T,axes=F,ylim=c(0,50),col=c(grey.colors(2)),axisnames=F ,xlab=("Sex and size"),ylab=("% mean coloration"),las=1)
axis(2,at=c(0,5,10,15,20,25,30,35,40,45,50),labels=c(0,5,10,15,20,25,30,35,40,45,50),las=1)
labs<-as.character(class)
text(graf,par("usr")[3]-0.25,srt=0,adj = c(0,2),labels=labs,xpd=T,cex=1)
legend(locator(1),c("Adult males","Adult females"),fill=c(grey.colors(2)),bty="n")

编辑:这里有一些可重现的代码:

structure(list(edad = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "ADU", class = "factor"), 
    sexo = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("H", "M"), class = "factor"), 
    clase.2 = structure(c(2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 
    6L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L), .Label = c("50-55", 
    "55-60", "60-65", "65-70", "70-75", "75-80"), class = "factor"), 
    perc.greenblue = c(0.09, 0.32, 12.8, 94.32, 34.83, 0.04, 
    45.83, 12.34, 0.75, 34.82, 0.5, 0.05, 3.46, 0, 1.72, 0.07, 
    0.09, 0.2)), row.names = c(9L, 10L, 12L, 13L, 48L, 49L, 109L, 
110L, 194L, 195L, 263L, 264L, 266L, 267L, 332L, 333L, 408L, 409L
), class = "data.frame")

标签: rlabelbar-chartaxis

解决方案


推荐阅读