Data Analysis

Are You Having Trouble Plotting a Time Series in R? Here’s one solution.

Context: I was using Gapminder.com data, so the data is presented in csv format.
The data consists of various country-related data with rows as country names and columns as years.

Step 1. Read into a data.frame the local .csv file that you downloaded from the Gapminder website.

Note that “row.names = 1” saves each of the 175 or so country names in the rows. Columns will be years.

df <- read.csv(‘path.to.your.local.file.here’, header = T, row.names = 1, check.names = F)

Step 2. Transform the data.frame. 

Years are now rows instead of columns. Country names are now cols.

df_t <- t(df)

Step 3. Add a “Year” column created from the row.names. For me, this was the missing link for
successfully plotting the data, else I was floundering.

Reason for this: We need this col to be numeric variables in order to plot
…which are the years in the time series

df_t$Year <- row.names(df_t)

Step 4. That’s it! Now you can plot. It’s that simple.

For example:

ggplot(df_t, aes(Year, group = 1)) +
geom_line(aes(y = Albania, color=“Albania”)) +
geom_point(aes(y = India, color=“India”))

Etc…

Peter Bakke - Data Analyst Gapminder.com plotting time series