Skip to content
Prev 166304 / 398502 Next

Plotting a graph for every Level of a Factor

On Wed, Jan 7, 2009 at 5:10 PM, jimdare <jamesdare26 at gmail.com> wrote:
It's not terribly easy to see exactly what you are trying to do (why
the combination of bars and lines?) but maybe this is close to what
you want:

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

fish <- read.table(textConnection("Year   Species Stock TACC Catch
2001       ORH     OR1     5000     4687
2002       ORH     OR1     6000     3215
2003       ORH     OR1     7000     6782
2004       ORH     OR1     9000     10000
2005       ORH     OR1     9000     12000
2001       ORH     OR3     20000           7892
2002       ORH     OR3     25000           27000
2003       ORH     OR3     30000           32000
2004       ORH     OR3     30000           29000
2005       ORH     OR3     30000           30000
2001       ORH     OR5     23000           10982
2002       ORH     OR5     23000           24590
2003       ORH     OR5     23000           24035
2004       ORH     OR5     25000           29008
2005       ORH     OR5     20000           21092"), header=T)

ggplot(fish, aes(x = Year)) +
  geom_line(aes(y = Catch, colour = "Catch")) +
  geom_line(aes(y = TACC, colour = "TACC")) +
  facet_grid(Species ~ Stock) +
  labs(colour = "??")

# or if you really want bars

ggplot(fish, aes(x = Year)) +
  geom_bar(aes(y = TACC), stat = "identity", fill = "grey70") +
  geom_line(aes(y = Catch)) +
  geom_point(aes(y = Catch)) +
  facet_grid(Species ~ Stock)

# this will split up by species too, but you only have one species
# in your example dataset

Hadley