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
status
major 1
minor 9.1
year 2004
month 06
day 21
language R
Thank you in advance for any help you can give me.
...heather
----------------------------------
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"):
Alpha,Beta,Gamma,Delta,Epsilon,Zeta,Eta,Theta,Xi,Omicron
2002,17.6,13.0,8.9,7.1,6.0,5.4,3.7,2.5,0,0
2003,0,11.6,0,0,8.7,6.4,0,3.3,10.2,7.9
2004,8.9,0,8.0,7.7,6.9,0,6.1,0,3.8,1.2
----------------------------------
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)
par(mar=c(6,4,6,4))
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
Heather J. Branton wrote:> 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..... Dear Heather - Please read Bill Cleveland's book The Elements of Graphing Data. A MUCH better plot can be produced. And let time be one of the first variables to vary. -- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University
On Mon, 2004-11-15 at 19:03 -0500, Heather J. Branton wrote:> 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.<snip> How about something like this: # Don't use 'sample' for the name here, as sample() is a function MyData <- t(read.table("sample.dat", sep= "," , header = TRUE)) # These may be closer to the PDF chart colors # You need to repeat them to color each group the same, rather # than alternating bar colors MyCols <- rep(c("lightcyan","cornsilk","lavender"), each = 10) # adjust the margins par(mar = c(7, 5, 6, 4)) # Now do the barplot: # Note barplot() returns the bar midpoints in 'mp' # use 'names.arg' for the individual bar names from MyData # set 'las = 2' for vertical labels # set 'ylim' to c(0, 20) for the y axis range # set 'yaxt = "n"' to not draw the y axis tick marks mp <- barplot(MyData, beside = TRUE, col = MyCols, main = "Rate by Group and Year", ylab = "Rate", names.arg = rep(rownames(MyData), 3), las = 2, cex.names = 0.75, ylim = c(0,20), yaxt = "n") # Now set up the y axis tick marks and labels ticks <- seq(0, 20, 2) axis(2, at = ticks, las = 1, labels = formatC(ticks, format = "f", digits = 1)) # Draw a box around the whole thing box() # Now draw the years. Note from ?barplot that colMeans(mp) are # the group midpoints mtext(side = 1, at = colMeans(mp), line = 3.5, text = colnames(MyData)) # Now draw the x axis label mtext(side = 1, line = 5.5, text = "Test and Year") Hope that gets you what you need. You can adjust the font sizes, etc. as you require. Note that unlike Excel, the 0 (zero) columns are not dropped. :-) HTH, Marc Schwartz