[R] barplot x-axis problem

Marc Schwartz marc_schwartz at comcast.net
Fri Jan 26 00:36:58 CET 2007


On Thu, 2007-01-25 at 22:23 +0200, Lauri Nikkinen wrote:
> Hi R-users,
> 
> I'm new to R and I'm trying to make a barplot combined with two lines
> (refering to secondary y-axis). Bars should represent the number of
> transfused patients by age class and sex and lines should represent
> the amount of blood units given in age classes. I have now successfully made
> a barplot and used par(new=TRUE) to plot another empty graph at the top of
> the barplot.
> 
> #tab-table:
> #        ikar_new
> #sp       0-9 10-19 20-29 30-39 40-49 50-59 60-69 70-79 >80
> #  mies   227    93    79    92   195   451   560   577 132
> #  nainen 183    80   102   175    99   161   230   357 164
> 
> barplot(tab,
>         beside=TRUE,
>         col = c("black", "lightgrey"),
>         legend = rownames(tab),
>         ylim= c(0,800),
>         font.main = 4,
>         cex.names = 1.1,
>         main = "Transfused patients and trombocytes given by age and sex",
>         ylab="Number of transfused patients",
>         xlab="Age groups (years)")
> 
> axis(1, c(0,3.5+3*0:9), labels=FALSE, tick=TRUE)
> 
> par(new=TRUE)
> 
> #temp-table
> #  ikar_new mies nainen
> #1      0-9 2296   2224
> #2    10-19 1648   3508
> #3    20-29 2276   1464
> #4    30-39 1920   2600
> #5    40-49 3912   2020
> #6    50-59 6856   2872
> #7    60-69 8748   3592
> #8    70-79 7052   4916
> #9      >80 1436   1780
> 
> 
> plot(temp$mies, type="n", yaxt='n', xaxt='n', ann=FALSE)
> lines(temp$mies, col="blue", lwd=2)
> lines(temp$nainen, col="red", lwd=2)
> axis(4, at=NULL)
> 
> I have used lines() to draw the lines into the picture. How can I get the
> lines into the same x-axis and get the actual data points of the lines to
> be exactly in between the two barplot's bars (categories in x-axis)? Now the
> points which the lines connect are not in the middle of the groups in x-axis
> as I would want them to be. The bars in the barplot are not stacked. I'm
> sorry that I'm not able to give you the scripts to make those tables.

I suspect that this is what you might require:


# Get the maximum value for both sets of data
# divide the second set by 10 to normalize to the 
# range of the first set

Max.y <- max(tab, as.matrix(temp[, -1]) / 10)


# Now do the barplot using c(0, Max.y) for ylim
# Also save the bar midpoints in 'mp'
# See ?barplot

mp <- barplot(tab, beside=TRUE,
              col = c("black", "lightgrey"),
              legend = rownames(tab),
              ylim = c(0, Max.y),
              font.main = 4,
              cex.names = 1.1,
              main = "Transfused patients and trombocytes given by age and sex",
              ylab ="Number of transfused patients",
              xlab ="Age groups (years)")

axis(1, c(0, 3.5 + 3 * 0:9), labels = FALSE, tick = TRUE)


# Now add the lines, dividing the y values by 10
# to fit the y axis range to the first set of data
# Use colMeans(mp) for the x axis values, which will
# give the midpoints of each bar pair

lines(colMeans(mp), temp$mies / 10, col = "blue", lwd = 2)
lines(colMeans(mp), temp$nainen / 10, col = "red", lwd = 2)


# Now set the values for the right hand axis

at <- seq(0, 800, 200)


# Set the axis labels to y at * 10

axis(4, at = at, labels = at * 10)


There are multiple ways to accomplish drawing two sets of data with
differing ranges on the same plot. Typically they involve the
normalization of the data to common ranges and then adjustment of the
axis labelling accordingly.

HTH,

Marc Schwartz



More information about the R-help mailing list