1

I have a huge table and I want to make a plot of lets say two different rows in this table.

Below you can see a small overview of my data set.

I want to plot now for country 4 the production of the years 1,2 and 3 so that someone can see the change over time

The same with country 6

On the X axis shoud be the years and on the Y axis should be the values.

Can somebody help me?

Thanks in advance!

   country    year1        unit        year2        unit    year3
    1          5.1         tonnes       1.4         tonnes   5
    2          4.9         tonnes       1.4         tonnes   2
    3          4.7         tonnes       1.3         tonnes   3.5
    4          4.6         tonnes       1.5         tonnes   8
    5          5.0         tonnes       1.4         tonnes   8
    6          5.4         tonnes       1.7         tonnes   6
burton030
  • 405
  • 4
  • 8
  • 23

1 Answers1

4

These are the steps to create the plot.

The data:

dat <- read.table(text="country    year1      unit      year2      unit    year3
   1          5.1         tonnes       1.4         tonnes   5
   2          4.9         tonnes       1.4         tonnes   2
   3          4.7         tonnes       1.3         tonnes   3.5
   4          4.6         tonnes       1.5         tonnes   8
   5          5.0         tonnes       1.4         tonnes   8
   6          5.4         tonnes       1.7         tonnes   6", header = TRUE)
  1. Choose a subset of the data:

    subdat <- dat[dat$country == 4, c("year1", "year2", "year3")]
    
  2. Arrange the data in the long format:

    subdat_l <- data.frame(Value = unlist(subdat), Year = factor(1:3))
    
  3. Plot:

    plot(Value ~ Year, subdat_l)
    

enter image description here


If the actual data frame consists of the data of more than three years, you can use this general approach:

years <- grep("^year", names(dat), value = TRUE) # find the columns with the data
subdat <- dat[dat$country == 4, years]
subdat_l <- data.frame(Value = unlist(subdat),
                       Year = substr(years, 5, nchar(years)))
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • Thanks for your answer! But is there also a possibility to avoid writing all the names of the years in the subset? Because I have a timeline of over 50 years. – burton030 Nov 13 '12 at 10:16