[R] tick marks and barchart
Deepayan Sarkar
deepayan at stat.wisc.edu
Fri Oct 17 17:53:40 CEST 2003
On Thursday 16 October 2003 10:03, Stefán Hrafn Jónsson wrote:
> Dear R community.
>
> I have two problems with figures. First deals with short vector on the
> x-axis and the second with two-panel barchart.
> 1) For demonstration I create the following pseudo data for three years,
> 2001:2003. The indicated plot looks fine except for the number of tick
> marks on the x-axis. I get seq(2001,2003,0.5). I want three and only three
> tick marks to indicate we have measure once a year not two times each
> year. (Having year 2001.5 is not that nice anyway). I tried
> as.factor(2001:2003) but this did not do what I want. I have considered
> having no labels and plot the year with text(y=-b,x=(2001,2002,2003),
> (2001:2003) ) -b being some value less than 0. A simpler version is
> preferred.
You can suppress the axes during the first plot() call and then construct them
manually:
##---------------
demo1 <- matrix(nrow=3, ncol =2, log(c(7,3,2,4,5,6))/log(7) , dimnames
=list(as.character(2001:2003),
c("Group A","Group B")) )
par(lab=c(3, 6,7) ,las=1 )
plot(x = (2001:2003), y = demo1[,1]*100, type = "l",
lwd=3,ylim=c(0,100), xlab = "" , ylab="%",
axes = FALSE, frame.plot = TRUE)
axis(2)
axis(1, at = 2001:2003)
lines(x=(2001:2003), y=demo1[,2]*100,
lwd=3, xlab = "Year" )
##-----------------
A slightly better approach (not for your problem, but for what you are trying
to do) would be to use matplot instead:
##----------------
matplot(x = 2001:2003, demo1 * 100, type = "l", lwd=3,
ylim = c(0, 100), xlab = "", ylab="%",
axes = FALSE, frame.plot = TRUE)
axis(2)
axis(1, at = 2001:2003)
##----------------
> 2) For the second problem I want to use the same data but create a
> barchart with two bars (Group A and group B) for 2001, same two groups for
> 2002 and same two for 2003. Group A would have blue bars and Group B red
> bars.
> Would I use barchart() or panel.barchart()? Looking in help(barchart) I
> find that I need to define a formula. What would the x, y and g1 be in my
> case?
barchart() is the trellis/lattice function for drawing barcharts, the
corresponding base function is barplot. In this case, what you want should be
doable with
barplot(t(demo1), beside = TRUE)
HTH,
Deepayan
More information about the R-help
mailing list