Skip to content

Label

4 messages · Val, Jim Lemon

Val
#
Hi all,

I have a sample of data set,

dat <- read.table(header=TRUE, text='Lab count
A 24
B 19
C 30
D 18')

barplot(dat$count, names.arg=c("A", "B", "C","D"),
        col="blue",
        ylim = c(0,30),
        ylab = "Count",
        xlab = "Grade")

I want add the number of counts at the top of each bar plot. How can I do that?
Thank you in advance
#
Hi Val,

library(plotrix)
barpos<-barplot(dat$count, names.arg=c("A", "B", "C","D"),
         col="blue",
         ylim = c(0,30),
         ylab = "Count",
         xlab = "Grade")
barlabels(barpos,dat$count,prop=1)

Jim
On Fri, Apr 3, 2020 at 1:31 PM Val <valkremk at gmail.com> wrote:
Val
#
Thank you Jim,

Is it possible to format the label box? The labels(numbers) are
surrounded by a big square and wanted to remove it. I just want
display only the number.  I searched up the documentation  for
"barlabels" and there is no such example

barlabels(xpos,ypos,labels=NULL,cex=1,prop=0.5,miny=0,offset=0,...)

Thank you.
On Thu, Apr 2, 2020 at 9:38 PM Jim Lemon <drjimlemon at gmail.com> wrote:
#
Hi Val,
A good suggestion. The revised code is below and it will be in the
next version of plotrix.

barlabels<-function(xpos,ypos,labels=NULL,cex=1,prop=0.5,miny=0,offset=0,
 nobox=FALSE,...) {

 if(is.data.frame(ypos)) ypos<-as.matrix(ypos)
 if(is.null(labels)) labels<-ypos
 # usually don't want to display zero labels
 display<-ypos > miny
 if(is.matrix(ypos)) {
  # prop is within the scope of the current environment
  cumcenter<-function(x,pos) return(cumsum(x)-x*prop)
  stacked<-length(xpos) < length(ypos)
  if(stacked) {
   # replicate the x positions one by one, but the offsets group by group
   xpos<-rep(xpos,each=length(ypos)/length(xpos))+
    rep(c(-offset,offset),length(ypos)/(2*length(xpos)))
   ypos<-apply(ypos,2,cumcenter)
  }
  else ypos<-ypos*prop
 }
 else ypos<-ypos*prop
 # allow labels to extend beyond the plot area
 par(xpd=TRUE)
 if(nobox) text(xpos[display],ypos[display],labels[display],cex=cex,...)
 else boxed.labels(xpos[display],ypos[display],labels[display],cex=cex,...)
 par(xpd=FALSE)
}

Just set "nobox" to TRUE. You can add a "col=" argument at the end and
it will be passed to "text".

Jim
On Sat, Apr 4, 2020 at 5:20 AM Val <valkremk at gmail.com> wrote: