Graphics Reference
In-Depth Information
[ 31 ] 51.0 50.6 51.7 51.5 52.1 51.3 51.0 54.0 51.4 52.7 53.1 54.6 52.0 52.0 50.9
[ 46 ] 52.6 50.2 52.6 51.6 51.9 50.5 50.9 51.7 51.4 51.7 50.8 51.9 51.8 51.9 53.0
# Get times for each observation
as.numeric(time(nhtemp))
[ 1 ] 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
[ 16 ] 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
[ 31 ] 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956
[ 46 ] 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
# Get value of each observation
as.numeric(nhtemp)
[ 1 ] 49.9 52.3 49.4 51.1 49.4 47.9 49.8 50.9 49.3 51.9 50.8 49.6 49.3 50.6 48.4
[ 16 ] 50.7 50.9 50.6 51.5 52.8 51.8 51.1 49.8 50.2 50.4 51.6 51.8 50.9 48.8 51.7
[ 31 ] 51.0 50.6 51.7 51.5 52.1 51.3 51.0 54.0 51.4 52.7 53.1 54.6 52.0 52.0 50.9
[ 46 ] 52.6 50.2 52.6 51.6 51.9 50.5 50.9 51.7 51.4 51.7 50.8 51.9 51.8 51.9 53.0
# Put them in a data frame
nht <- data.frame(year = as.numeric(time(nhtemp)), temp = as.numeric(nhtemp))
nht
year temp
1912 49.9
1913 52.3
...
1970 51.9
1971 53.0
Discussion
Time series objects efficiently store information when there are observations at regular time in-
tervals, but for use with ggplot2, they need to be converted to a format that separately represents
times and values for each observation.
Some time series objects are cyclical. The presidents data set, for example, contains four ob-
servations per year, one for each quarter:
presidents
Qtr1 Qtr2 Qtr3 Qtr4
1945
NA
87
82
75
1946
63
50
43
32
1947
35
60
54
55
...
Search WWH ::




Custom Search