[R] xlims of barplot
MSchwartz at medanalytics.com
Fri Nov 14 04:08:44 CET 2003
On Thu, 2003-11-13 at 18:01, Paul Sorenson wrote:
> >OK...I think I understand what you are doing.
> >You want a series of barplots that have "space" for the same number of
> >vertical bars along the x axis, but there may be gaps in the series for
> >any given barplot. Presumably, those gaps may be anywhere in the time
> >series along the x axis.
> Correct and most problematically at the ends.
Actually, in many respects, that makes it a bit easier... :-)
> >Hint: barplot() will leave gaps in the bar series where an NA appears in
> >the vector or in a matrix column of height values.
> >So, the key is to be sure that the vector or matrix has the same number
> >of elements or matrix columns in each dataset. For your incomplete
> >datasets, pad each series with NA's to fill out the missing entries in
> >the time series.
> That sounds like a way forward. I just need to go back to the basics and
> learn how to add "rows" to data.frames. I am sure it won't be hard, its
> just my personal learning curve with several new data types (factors,
> tables, data.frames vs vectors, lists, arrays which I am more familiar
> with). For example, yesterday I tried max(myFactor) and it gave me an
> error (something like "must be a vector"), even though to my naive way of
> thinking myFactor clearly had a numeric max.
What was myFactor? If it was a 'factor', then you cannot use max() on
it. Use str(myFactor) and see what it says. That will show you the
structure of myFactor.
The key will be to create the actual vector or matrix of bar values that
you will use for the barplots from your data frame. Don't modify the
dataframe, but extract the data you need and work with the subset.
Once you have that, it becomes relatively easy.
If for example, you have a single vector of 6 values and you need it to
have 3 NA's (gaps) before and 6 NA's (gaps) after, for a total of 15
SixValues <- c(12, 24, 11, 8, 17, 7)
MyData <- c(rep(NA, 3), SixValues, rep(NA, 6))
MyData ends up looking like:
 NA NA NA 12 24 11 8 17 7 NA NA NA NA NA NA
See ?rep and ?c for more information on generating repeating sequences
of values and concatenating (joining) vectors.
On the other hand, let's say you have data that requires segmented bars
with 15 bars, three segments each. However, you only have data for 6
bars and need to pad space for 3 bars before and 6 bars after:
SixBars <- matrix(c(4, 2, 6, 12, 8, 4,
3, 5, 3, 2, 5, 1,
5, 4, 8, 3, 2, 2), ncol = 6)
The above generates a matrix of values that looks like:
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 4 12 3 2 5 3
[2,] 2 8 5 5 4 2
[3,] 6 4 3 1 8 2
We end up with 3 rows and 6 columns, which will result in 6 bars, each
with 3 segments. Remember that in barplot(), the columns represent each
bar if 'beside = FALSE' or groups of bars if 'beside = TRUE'.
Now call barplot():
Now, we need to add the gaps before and after. In essence, we need to
add a 3 x 3 matrix of NA's before and a 3 x 6 matrix of NA's after to
pad the known data:
MyData <- cbind(matrix(rep(NA, 9), ncol = 3),
matrix(rep(NA, 18), ncol = 6))
MyData looks like:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] NA NA NA 4 12 3 2 5 3 NA NA NA
[2,] NA NA NA 2 8 5 5 4 2 NA NA NA
[3,] NA NA NA 6 4 3 1 8 2 NA NA NA
[,13] [,14] [,15]
[1,] NA NA NA
[2,] NA NA NA
[3,] NA NA NA
Now call barplot:
See ?cbind for more information on binding columns and rows (rbind).
And...finally, if you need the bars to be grouped:
barplot(MyData, beside = TRUE)
One final example. We need to take SixBars and add two gaps between the
3rd and 4th bars to make a total of 8:
MyData <- cbind(SixBars[, 1:3],
matrix(rep(NA, 6), ncol = 2),
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 4 12 3 NA NA 2 5 3
[2,] 2 8 5 NA NA 5 4 2
[3,] 6 4 3 NA NA 1 8 2
The construct SixBars[, 1:3] extracts all rows and the 1st through 3rd
columns from SixBars. See ?Extract for more information.
Be sure to read through the available documentation, especially
Introduction to R, which is available via the Documentation links on the
main R web site. That covers a lot of the data management basics. There
are also some very good user written documents on the Contributed
More information about the R-help