1

I'm trying to plot a zoo object in R. I've succeeded at plotting the data set like I want, but I can figure out how to change the panel labels (direction, size, order, etc.).

Very small subset of larger data set:

AvgRW.z <- structure(c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.933, 1.018, 
0.6415, 0.7495, 0.654166666666667, 0.635333333333333, 0.44875, 
0.615583333333333, 0.825857142857143, 0.7095, 1.1536875, 1.65311111111111, 
1.50425, 1.87955, 2.14577272727273, 1.61231818181818, 2.48027272727273, 
1.82383336363636, 1.373, 1.36390909090909, 1.38969441666667, 
1.39786108333333, 2.14262823076923, 2.37315478571429, 2.37939285714286, 
1.85764285714286, 1.98335714285714, 1.34670237857143, 0.710416664285714, 
0.657595235714286, 1.3541905, 1.81747621428571, 1.88546428571429, 
1.92221428571429, 2.10236907142857, 1.62090476428571, 1.61060714285714, 
2.03883335714286, 2.10155130769231, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 0.999, 0.671, 0.638, 0.767, 0.839, 1.072, 
1.162, 0.7215, 0.416, 0.3265, 0.464, 1.492, 1.011, 1.0905, 1.0755, 
1.42183333333333, 1.17783333333333, 0.931166666666667, 1.761375, 
2.063875, 1.91758333333333, 0.945583333333333, 0.60775, 1.07725, 
1.85278571428571, 2.05792857142857, 2.6515, 2.25066666666667, 
2.19533333333333, 1.931, 2.31927777777778, 2.5933, 3.03875, 2.66533333333333, 
2.70753846153846, 2.53957692307692, 2.98446153846154, 3.24507692307692, 
3.23284615384615, 3.59303846153846, 4.03557692307692, 3.45111538461538, 
3.47661538461538, 2.96003846153846, 2.96257692307692, 2.65265384615385, 
2.53873076923077, 2.60619230769231, 2.26838461538462, 1.96623076923077, 
2.27869230769231, 2.00357692307692, 1.74626923076923, 2.61165384615385, 
1.081, 0.739, 0.635, 1.813, 1.349, 1.479, 0.964, 1.029, 0.792, 
0.474, 0.428, 0.284, 0.253, 0.257, 0.388, 0.357, 0.316, 0.305, 
0.33, 0.258, 0.263, 1.135, 1.232, 0.648, 1.325, 1.461, 1.414, 
1.054, 0.401, 0.35, 0.498, 0.702, 0.636, 0.327, 0.276, 0.266, 
0.318, 0.305, 0.312, 0.248, 0.265, 0.253, 0.242, 0.224, 0.233, 
0.225, 0.262, 0.228, 0.356, 0.454, 0.474, 0.474, 0.466, 0.443, 
0.56, 0.455, 1.427, 0.777, 0.804, 1.376, 0.812, 0.728, 0.494, 
0.49, 0.733, 0.541, 0.57, 0.738, 0.469, 0.351, 0.439, 0.493, 
0.392, 0.489, 0.372, 0.544, 0.424, 0.514, 0.652, 0.383, 0.902, 
1.351, 0.963, 0.724, 0.658, 0.396, 0.302, 0.302, 0.213, 0.231, 
0.269, 0.264, 0.252, 0.207, 0.188, 0.072, 0.141, 0.218, 0.223, 
0.216, 0.199, 0.195, 0.048, 0.17, 0.18, 0.244, 0.183, 0.213, 
0.215, 0.127, 0.184, 0.227, 0.167, 0.12, 0.181, 0.143, 0.17, 
0.136, 0.191, 0.108, 0.183, 0.218, 0.213, 0.283, 0.208, 0.162, 
0.109, 0.116, 0.09, 0.116, 0.13, 0.172, 0.241, 0.186, 0.263, 
0.162, 0.155, 0.18, 0.3515, 0.3585, 0.4555, 0.407, 0.806, 0.61475, 
0.78375, 0.571, 0.4595, 0.572, 0.6015, 0.5395, 0.57125, 0.58925, 
0.628, 0.5235, 0.47725, 0.539, 0.5475, 0.6615, 0.6915, 0.903, 
0.817, 1.3595, 1.08216666666667, 1.40833333333333, 1.5035, 1.851, 
1.17716666666667, 1.15783333333333, 0.969833333333333, 0.861, 
1.22116666666667, 1.36016666666667, 1.1545, 0.700166666666667, 
1.37333333333333, 0.947666666666667, 1.05466666666667, 1.27183333333333, 
1.73233333333333, 1.02633333333333, 1.141, 1.51533333333333, 
1.51983333333333, 1.5285, 1.33866666666667, 1.46666666666667, 
1.58666666666667, 1.79483333333333, 1.7285, 1.54966666666667, 
1.73166666666667, 1.64666666666667, 1.21983333333333, 1.18183333333333, 
1.18233333333333, 1.4265, 1.57733333333333, 1.58483333333333, 
1.2885, 1.32116666666667, 1.17866666666667, 1.34, 1.9625, 1.67416666666667, 
1.74066666666667, 1.264, 1.50083333333333, 1.305625, 2.11375, 
1.911625, 2.00275, 2.288125, 2.208125, 1.905125, 2.198375, 2.440375, 
2.25475, 2.4833, 2.0895, 1.82585714285714, 2.04985714285714, 
2.08471428571429, 2.06178571428571, 2.03742857142857, 2.175375, 
1.602, 1.693625, 1.4650625, 1.45575, 1.318125, 1.51175, 1.509875, 
1.33025, 1.403875, 1.7210625, 1.3410625, 1.549375, 1.73025, 1.419625, 
1.3186875, 1.0964375, 1.2894375, 1.2999375, 1.2965625, 1.4950625, 
1.6164375, 1.11575, 0.993, 0.950125, 0.86375, 0.909363636363636, 
1.16231818181818, 1.04127272727273, 1.26325, 1.076125, 1.65279166666667, 
1.33608333333333, 1.78991666666667, 1.368, 1.21133333333333, 
1.65404166666667, 1.135, 0.592375, 0.524791666666667, 0.639583333333333, 
0.844625, 0.796416666666667, 0.938666666666667, 1.14479166666667, 
1.223, 0.826875, 0.859333333333333, 1.13633333333333), .Dim = c(273L, 
3L), .Dimnames = list(NULL, c("BR8", "FA5", "FK2")), index = c("1739", 
"1740", "1741", "1742", "1743", "1744", "1745", "1746", "1747", 
"1748", "1749", "1750", "1751", "1752", "1753", "1754", "1755", 
"1756", "1757", "1758", "1759", "1760", "1761", "1762", "1763", 
"1764", "1765", "1766", "1767", "1768", "1769", "1770", "1771", 
"1772", "1773", "1774", "1775", "1776", "1777", "1778", "1779", 
"1780", "1781", "1782", "1783", "1784", "1785", "1786", "1787", 
"1788", "1789", "1790", "1791", "1792", "1793", "1794", "1795", 
"1796", "1797", "1798", "1799", "1800", "1801", "1802", "1803", 
"1804", "1805", "1806", "1807", "1808", "1809", "1810", "1811", 
"1812", "1813", "1814", "1815", "1816", "1817", "1818", "1819", 
"1820", "1821", "1822", "1823", "1824", "1825", "1826", "1827", 
"1828", "1829", "1830", "1831", "1832", "1833", "1834", "1835", 
"1836", "1837", "1838", "1839", "1840", "1841", "1842", "1843", 
"1844", "1845", "1846", "1847", "1848", "1849", "1850", "1851", 
"1852", "1853", "1854", "1855", "1856", "1857", "1858", "1859", 
"1860", "1861", "1862", "1863", "1864", "1865", "1866", "1867", 
"1868", "1869", "1870", "1871", "1872", "1873", "1874", "1875", 
"1876", "1877", "1878", "1879", "1880", "1881", "1882", "1883", 
"1884", "1885", "1886", "1887", "1888", "1889", "1890", "1891", 
"1892", "1893", "1894", "1895", "1896", "1897", "1898", "1899", 
"1900", "1901", "1902", "1903", "1904", "1905", "1906", "1907", 
"1908", "1909", "1910", "1911", "1912", "1913", "1914", "1915", 
"1916", "1917", "1918", "1919", "1920", "1921", "1922", "1923", 
"1924", "1925", "1926", "1927", "1928", "1929", "1930", "1931", 
"1932", "1933", "1934", "1935", "1936", "1937", "1938", "1939", 
"1940", "1941", "1942", "1943", "1944", "1945", "1946", "1947", 
"1948", "1949", "1950", "1951", "1952", "1953", "1954", "1955", 
"1956", "1957", "1958", "1959", "1960", "1961", "1962", "1963", 
"1964", "1965", "1966", "1967", "1968", "1969", "1970", "1971", 
"1972", "1973", "1974", "1975", "1976", "1977", "1978", "1979", 
"1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", 
"1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", 
"1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", 
"2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011"
), class = "zoo")

Current code:

my.panel <- function(x, ...) {
  lines(x, ...)
  panel.number <- parent.frame()$panel.number
  abline(h=2.0, col = "black", lty = "dotted", lwd = 0.5)
  abline(v=seq(1750,2010,50),col = "lightgray", lty = "dotted", lwd = par("lwd"))
}

plot.zoo(AvgRW.z, plot.type="multiple", yax.flip=TRUE, nc=1, 
         screens=c(3,1,6,11,7,21,24,12,8,9,2,22,14,19,13,17,20,4,15,5,16,23,10,18),
         bty="o", main = "Title", type = "l", lwd = 1.5, col = "black", panel = my.panel, ylim=c(0,4.5), yaxt="n")

This is the current output graph (using my full data set): Sample

Ideally, I'd like to rotate the panel labels so they're horizontal and much smaller. I'm also trying to get the label order to match the screen order. I'd also like to be able to adjust the X-axis label "Index."

I've tried multiple options, including:

par.strip.text

panel.text

xy.labels

sort(AvrRW.z)

Either I'm not adding the code in at the right location, or I'm not using it correctly, because nothing has worked to change my labels or order my panels (the manual screen solution is the only thing that's worked to order the panels correctly, just not the labels).

Any suggestions would be helpful!

KKL234
  • 367
  • 1
  • 5
  • 23
  • Code posted in SO should be reproducible which includes showing all inputs and library statements. See these 3 links: http://stackoverflow.com/help/mcve http://stackoverflow.com/help/how-to-ask http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – G. Grothendieck Oct 23 '15 at 14:31

1 Answers1

1

The data posted in the question uses character strings as time values so first convert those to numeric:

library(zoo)
z <- AvgRW.z
time(z) <- as.numeric(time(z))

1) You may find it easier to use lattice or ggplot2, both of which are supported by zoo. Using lattice this makes the labels smaller and puts them across the top of each panel so it's not necessary to rotate them:

 library(lattice)
 xyplot(z, par.strip.text = list(cex = .5), 
    scales = list(y = list(relation = "same", alternating = TRUE)))

Also note that the number of panel columns can be specified by adding layout = c(2, NA) which specifies two columns.

screenshot

2) Another possibility is to put the panel labels in the upper left of the plot area itself so that they do not take up any extra room. This is even more compact than the prior solution since it only uses space that would have been empty anyways and does not use extra space for any labels.

library(grid)
library(lattice)

xyplot(z, strip = FALSE, panel = function(...) {
  grid.text(names(z)[which.packet()], .05, .95, gp = gpar(cex = 0.5))
  panel.xyplot(...)
}, scales = list(y = list(relation = "same", alternating = TRUE)))

screenshot

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • This is similar to the graph I have, but it doesn't use the code I posted in my original question. As mentioned in the question, I have a MUCH larger data set that produces 24 panels. When I run the code, the panel labels on axis 2 and 4 are too big to read. I was trying to figure out how to change the size of those labels. – KKL234 Oct 23 '15 at 16:55
  • I have revised the answer to use `xyplot.zoo` to make the labels smaller. Lattice by default puts them across the top of the panels which makes them horizontal by default. In general questions should be minimal and not include a bunch of irrelevant complicating arguments but rather the code should be reduced to focus on the core of the question. – G. Grothendieck Oct 23 '15 at 17:38
  • I appreciate the edit and explanation. I'm trying to create a very specific graph, and would love to learn how to manipulate it efficiently. In my original code, the panel labels are on the left and right of each panel, alternating sides. I'd love to keep this format if possible. My goal is to figure out how I can rotate the panel labels (to horizontal instead of vertical, but still on axis 2 and 4) and adjust the text size. – KKL234 Oct 23 '15 at 17:49
  • If you are willing to use grid you can get anything but this generally takes more time to create the code due to its lower level nature so I have provided another approach which only uses grid minimally and produces output that is very compact since the label is placed into space that would have been empty otherwise assuming an increasing time series. – G. Grothendieck Oct 23 '15 at 18:45
  • Have updated both answers after discussing with Deepayan. – G. Grothendieck Oct 27 '15 at 19:46