[R] Plotting from a list

Marc Schwartz marc_schwartz at comcast.net
Thu Nov 13 23:55:08 CET 2008


on 11/13/2008 03:30 PM Steve_Friedman at nps.gov wrote:
> Hi again,
> 
> Yes you are right   I should have included an example of the file
> 
> It looks like this after reading in the data and using tapply I arrive at
> the following
> 
>> Alt5rimc_mean
>              A      B          C          D          E          F
> 1965  8.423645  0.208  1.6296296  0.0000000  0.0000000  0.0000000
> 1966 30.881773 27.344 13.2592593 26.2962963 22.3090909 22.4545455
> 1967 29.783251 15.824 24.3333333 26.3333333 19.5090909 34.4545455
> 1968 17.827586 31.776 14.7777778 23.8888889 25.2000000 21.7272727
> 1969 27.507389 30.520 12.2222222 30.8518519 25.8363636 13.7272727
> 1970 17.467980 27.232 34.2592593 42.4074074 45.0000000 34.0909091
> 1971  8.167488  0.000  1.4074074  0.0000000  0.0000000  0.0000000
> 1972 32.502463 19.736 34.6666667 39.8888889 28.5272727 43.6363636
> ...
> 2000 44.610837 11.208  1.7777778 15.6296296 17.8545455  7.5454545
> 
> I do not have example code, to share, but I was initially thinking that it
> should be approached with as follows:
> 
> for (i in 1 to 6)             #  index for columns
>   for (j in 1 to 36)        # index for rows
> 
> barplot(data[i][j])
> 
> 
> But that is very crude and of course it does not work.  Hence the request
> for assistance.

A barplot is not a good way to display continuous data. It is suitable
for counts and proportions and even there, many would propose that you
use a dotplot or dotchart instead, given the high "ink to data" ratio
intrinsic to barplots.

Briefly, to display your data above using a barplot, with your data in a
dataframe called 'DF':

  barplot(t(DF), beside = TRUE)

That will provide for groups of bars by year, with each group containing
bars representing the values for each lettered measure. If you want the
groups to be by letters and the years thus within each lettered group:

  barplot(as.matrix(DF), beside = TRUE)



Continuing the barplot approach, you would be better using lattice
graphics here, with barchart() instead. However, first, we need to
restructure the data into a 'long' format. See ?reshape

 DF.long <- reshape(DF, varying = list(1:6), ids = rownames(DF),
                    times = colnames(DF), direction = "long")


This gives you data in the following format:

> head(DF.long, 16)
       time         A   id
1965.A    A  8.423645 1965
1966.A    A 30.881773 1966
1967.A    A 29.783251 1967
1968.A    A 17.827586 1968
1969.A    A 27.507389 1969
1970.A    A 17.467980 1970
1971.A    A  8.167488 1971
1972.A    A 32.502463 1972
1965.B    B  0.208000 1965
1966.B    B 27.344000 1966
1967.B    B 15.824000 1967
1968.B    B 31.776000 1968
1969.B    B 30.520000 1969
1970.B    B 27.232000 1970
1971.B    B  0.000000 1971
1972.B    B 19.736000 1972


Now, you can do something like:

  library(lattice)
  barchart(A ~ time | id, data = DF.long)

or:

  barchart(A ~ id | time, data = DF.long)

depending upon what you are trying to show.


However, I would also give due consideration to something like the
following:

  dotplot(A ~ time | id, data = DF.long)

or:

  dotplot(A ~ id | time, data = DF.long)


You can read up on lattice graphics and the additional parameters and
functions that provide for flexibility in things like text sizes and so
forth, in much the same way as in base graphics.

Good references on lattice graphics would be:

Paul Murrell's book "R Graphics":
http://www.crcpress.com/shopping_cart/products/product_detail.asp?sku=C486X

and

Deepayan Sarkar's book "Lattice Multivariate Data Visualization with R":
http://www.springer.com/statistics/computational/book/978-0-387-75968-5



HTH,

Marc Schwartz



More information about the R-help mailing list