2014-09-26 63 views
2

我有一个数据帧称为fin用R绘制一个数据框的子集?

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 

我想绘制FR.CoYear针对(fin$Young/fin$Sample)单独为每个所述5种在$Species

我试过了建议的方式here;但目前没有任何工作,我会非常感谢指导 - 这只是一个语法问题?

这是我曾尝试:

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 

我道歉,如果这是很基本的,但我自学R.

+0

以供将来参考,这将是更容易帮助你,如果你提供一个[重复的例子(http://stackoverflow.com/questions/5963269/how-to-make-a-大-R重现-例子)。在这种情况下,这意味着您的数据样本(除了您已经提供的代码和解释)。 – eipi10 2014-09-26 17:00:54

回答

0

没有你的数据的样本,我无法测试的答案下面,但你必须在你的代码,我试图修正一些错误:

  1. 当您使用withsubset你不需要重申名称数据框的当您引用个别列时。

    原始代码:

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

    更改为:

    with(subset(fin, Species == "TRAT"), plot(FR.CoYear, Young/Sample)) 
    
  2. 在这里,你放错地方的除了括号对于不需要在调用数据帧的名义重申对plot

    原创编码:

    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," 
    

    更改为:

    with(fin[fin$Species == "TRAT",], plot(FR.CoYear, Young/Sample)) 
    
  3. fin$Young也必须由Species

    原始代码进行索引:

    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 
    

    更改为:

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

如果您愿意学习ggplot2,您可以轻松地为物种的每个值创建单独的图。

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 ~ .) 
+0

谢谢你eipi10,这是一个非常有用的答案和ggplot2非常有用的提示。我现在有一些非常好的情节。谢谢。 – Mootie 2014-09-27 15:30:21

+0

很高兴有帮助。 – eipi10 2014-09-27 17:06:56

0

你可以试试这个:例如(再次,我不能没有你的数据的样本测试这个)

基本情节,即对两个品种:

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

添加更多物种只需添加更多点()。

ggplot2,i。e对于两种物种:

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

在这里添加更多物种只需在列表c()中添加参考。

我认为这会对你有效,只需在需要的情况下测试并更正var名称即可。

我最诚挚的问候