R 条形图,而不是条形图,删除 yaxis 刻度

R barchart, not barplot, remove yaxis ticks

我正在尝试从条形图中删除 y 轴刻度。

google 立即将 barchart 更正为 barplot,一点用处都没有。

yaxt="n" 无法使用条形图...

有人知道如何在 R 中删除条形图中的 yaxis 刻度吗?

我需要条形图,因为这是我能找到的以我想要的方式对数据进行分组的唯一方法...

MWE 在这里:

 library(lattice)

 molnames<-c("A","B","D","G","C","F")
 contactcounts<-c(1,2,3, 6,12,18,4,8,16,10,20,30,2,4,8,3,6,9)

 Acolumn1=factor(rep(molnames, each=3), levels=molnames )
 Acolumn2=rep(c("test1","test2","test3"), 6)
 Acolumn3=contactcounts
 colour<-c("orange", "blue","magenta")
 tiff(file="./testingABC.tiff", res=1000, width = 8, height = 8,units='in')
 trellis.par.set("grid.pars"=list(fontfamily="serif"))
 barchart(Acolumn3 ~ Acolumn1,ylab="y axis", yaxt="n", groups=Acolumn2,  auto.key = list(columns = 3),  par.settings=list(superpose.polygon=list(col=colour)))

请注意,您使用的是 lattice 包中的函数,而不是 base 包中的函数,并且它具有不同的参数。
要完成你想要的,你应该设置 scales 参数(参见 ?barchart 文档);有两个选项给出了略有不同的结果:

# option 1: we're saying that y ticks must be set at coordinate = NULL
barchart(Acolumn3 ~ Acolumn1,ylab="y axis", groups=Acolumn2,  auto.key = list(columns = 3),  
         scales=list(y=list(at=NULL)),
         par.settings=list(superpose.polygon=list(col=colour)))

# option 2: we're saying not to draw y axis
barchart(Acolumn3 ~ Acolumn1,ylab="y axis", groups=Acolumn2,  auto.key = list(columns = 3),  
         scales=list(y=list(draw=FALSE)),
         par.settings=list(superpose.polygon=list(col=colour)))


这是一个如何使用基数 R 绘制条形图的示例:

# Acolumn1,Acolumn2,Acolumn3 have been created in your example
DF <- data.frame(Acolumn1,Acolumn2,Acolumn3)

###### build matrix to be passed to barplot using R base
reshaped <- reshape(DF, idvar="Acolumn1",timevar="Acolumn2", direction = "wide",sep='_')
names(reshaped) <- gsub('Acolumn3_','',names(reshaped))
reshapedMx <- as.matrix(reshaped[,-1])
rownames(reshapedMx) <- reshaped[,1]
reshapedMx <- t(reshapedMx)

###### build matrix to be passed to barplot using reshape2 package (less code)
# library(reshape2)
# reshapedMx <- acast(DF, Acolumn1 ~ Acolumn2, value.var='Acolumn3')
# reshapedMx <- t(reshapedMx)

colors <- rainbow(nrow(reshapedMx))
barplot(reshapedMx,beside = TRUE,col=colors,ylim=c(0,max(reshapedMx)*1.2), yaxt='n')
legend('top',fill=colors,legend=rownames(reshapedMx), horiz=TRUE)
# call box() if you want to add a box around the plot