Mootie Mootie - 3 months ago 18
R Question

Plotting a subset of a dataframe with R?

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.

Answer

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 ~ .)
Comments