[R] Question about 'lables' & ect.

Marc Schwartz MSchwartz at mn.rr.com
Fri Mar 17 03:18:03 CET 2006

On Thu, 2006-03-16 at 10:37 +0100, jia ding wrote:
> Thanks Marc & Liaw!
> On 3/15/06, Marc Schwartz (via MN) <mschwartz at mn.rr.com> wrote:
>         On Wed, 2006-03-15 at 17:54 +0100, jia ding wrote:
>         > Hi,
>         >
>         > I have a file named:
>         > test_R.txt
>         > aaa  2
>         > bbb  5
>         > ccc  7
>         > sss  3
>         > xxx  8
>         >
>         > I want to have a plot: 
>         > test<-read.table("test_R.txt",col.name=c("Name","Score"))
>         > par(mfrow=c(1,2))
>         It's not clear what the purpose is here, at least in this
>         example. Do
>         you plan on creating a second plot? 
> Yes, I want to plot the second one.
> e.g. the second file is :
> test2_R.txt
> zszs  2
> gjie  4
> gjai  5
> opwn  6
> jgio  3
> gjwn  8
> owln  6
> aonl  1
> gols  7
> I post the complete command here: 
> test<-read.table("/vol/fpsearch/jiading/berman/single/test_R.txt",col.name=c("Name","Score"))
> #single<-single[order(-single$Weights),]
> #decreastest_R.txt",col.name=c ("Name","Score")) 
> test2<-read.table("/vol/fpsearch/jiading/berman/single/test2_R.txt",col.name=c("Name","Score"))
> par(mfrow=c(2,1))
> t<-barplot(test$Score,space=0.5)
> axis(1,at=t,labels=as.character (test$Name))
> t2<-barplot(test2$Score,space=0.5)
> axis(1,at=t2,labels=as.character(test2$Name))
> P1; The problem is, even I set each plot with "space=0.5", the bar's
> width are different. The first plot's bars are lot wider than the
> second plot's. What I want is, to make 2 plots same width. 

The problem of course is that you have a different number of bars in
each plot. Note from ?barplot:

  space: the amount of space (as a fraction of the average bar width)
         left before each bar. 

While the average bar width in each plot is 1, this is a measure based
upon 1 unit on the x axis range. Since both plots are being drawn in the
same horizontal dimension, but have differing x axis ranges, a 1 in the
first plot is larger than a 1 in the second plot.

One possible workaround is to set the x axis ranges to the same value:

par(mfrow=c(2, 1))

t <- barplot(test$Score, space = 0.5, xlim = c(0, 14))
axis(1, at = t,labels = as.character(test$Name))

t2 <- barplot(test2$Score,space = 0.5, xlim = c(0, 14))
axis(1, at = t2,labels = as.character(test2$Name))

The possible downside of this approach is that it puts the bars in the
first plot offset to the left, with the remainder of the x axis space

Alternatively, you can increase the size of 'space' in the first plot to
reduce the width of the bars until the widths are comparable:

par(mfrow=c(2, 1))

t <- barplot(test$Score, space = 1.75)
axis(1, at = t,labels = as.character(test$Name))

t2 <- barplot(test2$Score, space = 0.5)
axis(1, at = t2,labels = as.character(test2$Name))

It just depends upon the "look" that you want and what aids in reader

>         > barplot(test$Score)
>         > name<-test$Name
>         > axis(1,at=1:length(test$Name),labels=paste(name)) 
>         >
>         > Q1, if you try the script above,you will get 5 bars, the
>         axis only shows
>         > "aaa", "ccc","xxx", but where are "bbb"&"sss"?
>         The easiest way to do this is to use the ' names.arg' argument
>         in
>         barplot():
>         barplot(test$Score, names.arg = as.character(test$Name)) 
> P2:  if I made 2 barplot next to each other by: par(mfrow=c(1,2)).  It
> seems " names.arg" doesn't work for the second plot ( test2_R.txt), as
> it automatically hides some of the labels. Of course, in order to draw
> everything, I can try to rotate the labels e.g. par(las=2).  While, I
> am thinking, it should have some solution to solve this problem
> without turning labels. 

The problem is that there is not enough horizontal space for the labels.
If you drag the size of the plot window horizontally, they should show
as you provide more physical space in the plot device for them.

Alternatively, if you set 'cex.axis' in the calls to axis() as below,
they will be small, but will be there.


t <- barplot(test$Score, space = 0.5)
axis(1, at = t,labels = as.character(test$Name), 
     cex.axis = 0.1)

t2 <- barplot(test2$Score,space = 0.5)
axis(1, at = t2,labels = as.character(test2$Name), 
     cex.axis = 0.1)

Of course, you can make them small enough that they are hard to read.
Thus, rotating the labels may not be a bad idea if this is the layout
you want.

> P3: if I typed pdf("test.pdf")
> Suppose the plot has lots of bars.  Sometimes,there is not enough
> space for pdf to draw everything. Which means it's not a complete
> file. Do you have any suggestion about it? 

Did you remember to call:


after all plot related calls are complete to close the PDF file?

If you don't, then the output of the final plot function related calls
do not get flushed to the file and you end up with an incomplete graph.

> PS: do you know, if there is the website which provides all the
> questions people are asking now?  I mean,  when I  have problems I
> send email to r-help at stat.math.ethz.ch, and then some nice people will
> have a look of my problems and send me back their solutions. I am
> puzzled, except daily R-help-request mail, how can I get other
> people's r-help mail simultaneous? I remember I do subscribe to the
> mail group. 

You might want to check the e-mail list site at:


to check the settings that you subscribed with. Go to the bottom of the
page and then login with your e-mail and then password when prompted.

Keep in mind that there are delays (at times longer than others) in
getting posts from the list. If your settings are appropriate, then you
should get all posts going to the list, possibly including your own.

In terms of resources, there are the official R Manuals which are a good
place to start. These are available in your R installation or via the R
web site under Documentation. useR Contributed documents are available
there as well.

Searching the list archives, either using the web page provided at:


or using:

  RSiteSearch("Your Keywords Here")

will be helpful. See ?RSiteSearch for more information.

In addition, there is an evolving wiki here:


and a R Graphics Gallery here:


Finally, reading the Posting Guide, for which there is a link at the
bottom of all posts can be insightful.


Marc Schwartz

More information about the R-help mailing list