0

I am trying to make a line graph of each row of my data frame traff The x axis would be the year from 2006 to 2012.

The final result should be a separate graph for each line. In the example below the first graph represents the third line and the second one the second line (see table below): enter image description here

I know how to find the max value of each row to get the max yaxis with:

xaxis <- colnames (traff[,(3:10)])

But I am struggling to make a loop for each row and generate a line graph. As you see each port (a, b and c) has three categories (IntPass, Pass and Cruise).

Port    Category    2006    2007    2008    2009    2010     2011    2012
a       IntPass     9046000 0       9579000 9683700 10404900 0       0
a       Pass        270000  260000  360000  360000  342000   385000  368000
a       Cruise      259     238     269     263     247      258     265
b       IntPass     8249304 8222336 8692362 9015726 9107665  9177075 9050424
b       Pass        0       272584  351267  437437  381141   407162  463770
b       Cruise      260     255     265     293     261      263     274
c       IntPass     6760000 6514294 7247366 7257646 7900000 8500000  8800000
c       Pass        305026  294738  377522  416605  392000  443000   442000
c       Cruise      289     268     298     305     279     293      294
Manuel Frias
  • 311
  • 1
  • 4
  • 10
  • Sorry, but I don't understand what the plot would look like. You say that you want to put the category on the y axis, but I would expect the values there. – Roland Dec 09 '14 at 15:39
  • 1
    @Roland After reading his description, I'm imagining three different plots(faceted by Category perhaps?) and each plot is a line plot (with 3 lines, one for each Port?) with the x-axis values being years. Not positive though. To Manuel, [I suggest looking at this question](http://stackoverflow.com/questions/3777174/plotting-two-variables-as-lines-using-ggplot2-on-the-same-graph/3777592#3777592). – Kyle S. Dec 09 '14 at 15:49
  • Thanks @Roland. Edited to clarify more the problem – Manuel Frias Dec 09 '14 at 19:32

3 Answers3

5

You should not use a for loop for drawing lines into a chart. The ggplot package does it for your if your category variable is a factor. Have a look at the following code:

library(reshape2) #install.packages("reshape2")
library(ggplot2) #install.packages("ggplot2")

#Import data into a Data frame
data <- c("
Port    Category    2006    2007    2008    2009    2010     2011    2012
a       IntPass     9046000 0       9579000 9683700 10404900 0       0
a       Pass        270000  260000  360000  360000  342000   385000  368000
a       Cruise      259     238     269     263     247      258     265
b       IntPass     8249304 8222336 8692362 9015726 9107665  9177075 9050424
b       Pass        0       272584  351267  437437  381141   407162  463770
b       Cruise      260     255     265     293     261      263     274
c       IntPass     6760000 6514294 7247366 7257646 7900000 8500000  8800000
c       Pass        305026  294738  377522  416605  392000  443000   442000
c       Cruise      289     268     298     305     279     293      294
")
traff <- read.table(text = data, header=TRUE)

#Reshape the data for ggplot
traff2 <- melt(traff,id=c("Port","Category"),variable.name = "Year")

#Remove the X in the Year column and convert it to number
traff2$Year <- as.numeric(gsub(pattern="X",replacement = "",x = as.character(traff2$Year)))

#The data is ready for ggplot
head(traff2)

#ggplot it...
ggplot(traff2, aes(x = Year, y = value, color = Port))+
facet_grid(facets = Category~., scales = "free_y")+
  geom_line()+theme_bw()

Result: enter image description here

Emer
  • 3,734
  • 2
  • 33
  • 47
1

Do you want them all on the same graph or a separate graph for each iteration of the loop?

Assuming on the same graph, you'd do something like this.

The plot function makes a new graph each time, so you have to use the lines() function to add to an existing graph.

# Make the initial plot
plot( 2006:2012, traff[1,3:ncol(traff)], type="l", col=1)

# Here is the loop where the rest is added
for (i in 2:nrow(traff)) { lines(2006:2012, traff[i,3:ncol(traff)], type="l", col=i)
                          }

You could use some if or while statements to add the initial plot within the loop if you want.

James Holland
  • 1,102
  • 10
  • 17
0

If I understand you correctly, there is a simple solution with matplot function:

matplot(t(traff[, -c(1,2)]), type="l")

If you want, you just have to change labels of x axis to names of the years and you are done.

Tim
  • 7,075
  • 6
  • 29
  • 58