2

I have RStudio and want to import a time series data set. The column on the x-axis should be the year, however when I use the ts.plot command it just plots Time on the x-axis. How can I make the years from the data set appear on my plot?

The data set is for Water Usage in NYC from 1898 to 1968. There are two columns, The Year and Water Usage.

This is the link to the data I used (I have donwnloaded the .TSV file)

https://datamarket.com/data/set/22tl/annual-water-use-in-new-york-city-litres-per-capita-per-day-1898-1968#!ds=22tl&display=line

These are the commands for importing my data:

nyc <- read.csv("~/Desktop/annual-water-use-in-new-york-cit.tsv", sep="")
View(nyc)
ts.plot(nyc)

This is what I get:

hpesoj626
  • 3,529
  • 1
  • 17
  • 25
Slam95
  • 23
  • 5
  • Just to "make sure" you could convert the x-axis using `as.POSIXct` or `as.Date`. These are classes made to use in time series plot – theBotelho Apr 16 '18 at 01:40
  • Welcome to SO. Please review [how to ask](https://stackoverflow.com/help/how-to-ask), and then provide a [minimal reproducible example/attempt](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). For questions of the form "I want to do XYZ", you need to show some effort and at the very least provide sample data. – Maurits Evers Apr 16 '18 at 01:54

1 Answers1

0

There are several ways to do this. I used the CSV file from your link in this demonstration.

library(tidyverse)
nyc <- read_csv("annual-water-use-in-new-york-cit.csv")
head(nyc)
# A tibble: 6 x 2
  Year  `Annual water use in New York city, litres per capita per day, 1898-1968`
  <chr> <chr>                                                                    
1 1898  402.8                                                                    
2 1899  421.3                                                                    
3 1900  431.2                                                                    
4 1901  426.2                                                                    
5 1902  425.5                                                                    
6 1903  423.6 

Method 1

Create a time series object and plot this time series.

Firstly, let us fix the column name of the annual water use so that it is easier to call in our code.

nyc <- nyc %>%
  rename(
    water_use = `Annual water use in New York city, litres per capita per day, 1898-1968`
  )

Make the time series object nyc.ts with the ts() function.

nyc.ts <- ts(as.numeric(nyc$water_use), start = 1898)

You can then use the generic plot function to plot the time series.

plot(nyc.ts, xlab = "Years")

Method 2

Use the forecast::autoplot function. Note that this function is built on top of ggplot2.

autoplot(nyc.ts) + xlab("Years") + ylab("Amount in Litres")

enter image description here

Method 3

With just ggplot2:

nyc$Year <- as.POSIXct(nyc$Year, format = "%Y")
nyc$water_use <- as.numeric(nyc$water_use)
ggplot(nyc, aes(x = Year, y = water_use)) + geom_line() + xlab("Years") + ylab("Amount in Litres")
hpesoj626
  • 3,529
  • 1
  • 17
  • 25