R条形图,而不是条形图,删除yaxis蜱

时间:2016-06-01 08:25:49

标签: r charts axis

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

google会立即将条形图更正为barplot,并且完全没有帮助。

yaxt =" N"没有使用条形图......

任何人都知道如何在R?

的条形图中删除yaxis刻度

我需要barchart,因为这是我能找到的唯一方法,可以让我按照我想要的方式对数据进行分组......

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)))

1 个答案:

答案 0 :(得分:1)

请注意,您使用的是格子包中的功能,而不是基础包中的功能,并且它具有不同的参数。
要完成您想要的任务,您应该设置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)))

enter image description here

# 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)))

enter image description here

以下是如何使用基础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

enter image description here