2

I have a data frame called fin:

str(fin)
'data.frame':   158 obs. of  9 variables:
 $ Species      : chr  "TRAT" "TRAT" "TRAT" "WRAT" ...
 $ Site         : chr  "BAN" "BEX" "BEX" "BEX" ...
 $ Year         : chr  "2011" "2010" "2011" "2012" ...
 $ FR.CoYear: num  35.7 123.6 136.4 215.8 145.2 ...
 $ Sample       : int  31 NA 929 809 NA NA NA 30 215 NA ...
 $ Young        : num  16 NA 828 709 NA NA NA 45 235 NA ...
 $ SiteYear     : Factor w/ 65 levels "BAN 2011","BAN 2012",..: 1 4 5 6 7 1

I would like to plot FR.CoYear against (fin$Young / fin$Sample) separately for each of the 5 species in $Species.

I tried the ways suggested here; but none are currently working, I would be very grateful for guidance - is this just a syntax problem?

This is what I have tried:

with(subset(fin,fin$Species == "TRAT"), plot(fin$FR.CoYear, fin$Young /fin$Sample))
 ## runs without error but no plot is produced

with(fin[fin$Species == "TRAT",], plot((fin$FR.CoYear, fin$Young / fin$Sample))
##gives the error: unexpected ',' in "with(fin[fin$Species == "TRAT",], plot((fin$FR.CoYear,"

plot(fin$FR.CoYear[fin$Species == "BLKI"],fin$Young / fin$Sample[fin$Species == "BLKI"])
##Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' and 'y' lengths differ 

I apologise if this is very basic, but am teaching myself R.

eipi10
  • 91,525
  • 24
  • 209
  • 285
Mootie
  • 23
  • 1
  • 3
  • For future reference, it will be easier to help you if you provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). In this case, that means a sample of your data (in addition to code and explanation you've already provided). – eipi10 Sep 26 '14 at 17:00

2 Answers2

1

Without a sample of your data, I can't test the answers below, but you have some errors in your code, which I've tried to fix:

  1. When you use with or subset you don't need to restate the name of the data frame when your refer to individual columns.

    Original code:

    with(subset(fin,fin$Species == "TRAT"), plot(fin$FR.CoYear, fin$Young /fin$Sample))
    

    Change to:

    with(subset(fin, Species == "TRAT"), plot(FR.CoYear, Young/Sample))
    
  2. Here you misplaced a parenthesis in addition to not needing to restate the name of the data frame in the call to plot:

    Original code:

    with(fin[fin$Species == "TRAT",], plot((fin$FR.CoYear, fin$Young / fin$Sample))
    ##gives the error: unexpected ',' in "with(fin[fin$Species == "TRAT",], plot((fin$FR.CoYear,"
    

    Change to:

    with(fin[fin$Species == "TRAT",], plot(FR.CoYear, Young / Sample))
    
  3. fin$Young must also be indexed by Species

    Original code:

        plot(fin$FR.CoYear[fin$Species == "BLKI"],fin$Young / fin$Sample[fin$Species == "BLKI"])
        ##Error in xy.coords(x, y, xlabel, ylabel, log) : 
          'x' and 'y' lengths differ
    

    Change to:

        plot(fin$FR.CoYear[fin$Species == "BLKI"], 
             fin$Young[fin$Species == "BLKI"]/ fin$Sample[fin$Species == "BLKI"])
    

If you're willing to learn ggplot2, you can easily create separate plots for each value of Species. For example (once again, I couldn't test this without a sample of your data):

library(ggplot2)

# One panel, separate lines for each species
ggplot(fin, aes(FR.CoYear, Young/Sample, group=Species, colour=Species)) + 
  geom_point() + geom_line()

# One panel for each species
ggplot(fin, aes(FR.CoYear, Young/Sample)) + 
  geom_point() + geom_line() +
  facet_grid(Species ~ .)
eipi10
  • 91,525
  • 24
  • 209
  • 285
  • Thank-you eipi10, that was a fantastically helpful answer and really useful tip with ggplot2. I have some very nice plots now. Thanks. – Mootie Sep 27 '14 at 15:30
0

You can try this:

Basic plot, i.e for two species:

plot(FR.CoYear ~ Young/Sample, data=subset(fin, Species == "TRAT"))
points(FR.CoYear ~ Young/Sample, col="red",data=subset(fin, Species == "WRAT"))

to add more species just add more points().

ggplot2, i.e for two species:

ggplot(subset(fin, Species %in% c("TRAT", "WRAT")),
       aes(x=FR.CoYear,
       y=Young/Sample,
       color=Species))+
  geom_point()

to add more species here just add the references on the list c().

I think this will work for you, just test and correct the var names if needed.

My best regards

André WZ
  • 141
  • 4