[R] Barplot difficulties

Heather J. Branton hjb at pdq.com
Tue Nov 16 01:03:24 CET 2004

Hello. I am an R newbie struggling to learn and use R . I have read many 
portions of the R Reference Manual, as well as the FAQs. Given that I 
learn something new each time, I know I might be missing something 
obvious. But I appeal to your good nature to help me through this 
initial problem.

I have attached a pdf file to demonstrate what I desire and have listed 
what my data looks like in Excel (below). Following is the data and 
script I developed - which does not provide what I want.

My immediate goal is to create a barplot in R similar to the attached 
pdf chart. But I am stuck on several problems. First, I would like to 
have 2 labels below the barplot - one label for each bar and one for 
each group of bars. Second, I would like to vary color by group (instead 
of by bar). I assume that I need to do use some sort of syntax within 
the color option but have not yet figured it out. I have made two 
different plot attempts -- one resulting in the bars being grouped 
appropriately but missing the labels below the x-axis; the other giving 
me the individual labels but not grouped as I need.

This is my version information:
platform i386-pc-mingw32
arch     i386
os       mingw32
system   i386, mingw32
major    1
minor    9.1
year     2004
month    06
day      21
language R

Thank you in advance for any help you can give me.



Spreadsheet data to obtain attached pdf:

Year  Group    Rate
2002  Alpha    17.6
      Beta     13.0
      Gamma     8.9
      Delta     7.1
      Epsilon   6.0
      Zeta      5.4
      Eta       3.7
      Theta     2.5

2003  Beta     11.6
      Epsilon   8.7
      Zeta      6.4
      Theta     3.3
      Xi       10.2
      Omicron   7.9

2004  Alpha     8.9
      Gamma     8.0
      Delta     7.7
      Episilon  6.9
      Eta       6.1
      Xi        3.8
      Omicron   1.2


R data set ("sample.dat"):



My R code attempt:

# Read in data
sample <- t(read.table("sample.dat", sep=",", header=T))

# Set color palette
shade <- palette(c("cyan2","yellow","magenta1"))

# Set plot limits:
ymax <- as.integer(max(sample)+1)

# Bar graph (get grouped plot but not grouped color nor individual 
labels below)
barplot(sample, beside=T, xlab="Test and Year", ylab="Rate", font.lab=3, 
axis.lty=1, col=shade)
legend(1,1, rownames(sample), xjust=-5.4, yjust=-2, col=shade, lty=1, lwd=2)
title(main="Rate by Test and Year", outer=F, font.main=2, line=3)

# Bar graph (get individual labels below but not grouped by year)
barplot(t(sample), beside=T, xlab="Test and Year", ylab="Rate", 
font.lab=3, axis.lty=1, las=2, col=shade)
legend(1,1, colnames(sample), xjust=-6, yjust=-7, col=shade, lty=1, lwd=2)
title(main="Rate by Test and Year", outer=F, font.main=2, line=3)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: sample.pdf
Type: application/pdf
Size: 36182 bytes
Desc: not available
Url : https://stat.ethz.ch/pipermail/r-help/attachments/20041115/ba818ad9/sample.pdf

More information about the R-help mailing list