Reputation: 115
I am trying to remove the y-axis ticks from a barchart.
google immediately corrects barchart to barplot, and is not helpful at all.
yaxt="n" is not working with barchart...
Anyone have an idea how to remove the yaxis ticks in barcharts in R?
I need barchart because this is the only way I could find that works for me to group my data in the way I want it...
An MWE is here:
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)))
Upvotes: 0
Views: 1613
Reputation: 57230
Please note that you're using a function from lattice package, not from base package and it has different parameters.
To accomplish what you want, you should set scales
parameter (see ?barchart
documentation); there are two options giving a slighlty different result :
# 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)))
Here's an example how to do a barplot using base 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
Upvotes: 1