用R?绘制数据帧的子集?

用R?绘制数据帧的子集?,r,syntax,plot,subset,R,Syntax,Plot,Subset,我有一个名为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

我有一个名为
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
我想针对
$species
中的5种物种分别绘制
FR.CoYear
(fin$Young/fin$Sample)

我尝试了建议的方法;但是目前没有一个可以工作,我非常感谢您的指导-这只是一个语法问题吗

这就是我尝试过的:

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。

没有您的数据样本,我无法测试以下答案,但您的代码中有一些错误,我已尝试修复这些错误:

  • 当您将
    子集一起使用时,您不需要重述名称
    当引用单个列时,数据帧的

    原始代码:

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

    with(subset(fin, Species == "TRAT"), plot(FR.CoYear, Young/Sample))
    
    with(fin[fin$Species == "TRAT",], plot(FR.CoYear, Young / Sample))
    
        plot(fin$FR.CoYear[fin$Species == "BLKI"], 
             fin$Young[fin$Species == "BLKI"]/ fin$Sample[fin$Species == "BLKI"])
    
  • 在这里,除了不需要在调用
    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,"
    
        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
    
    改为:

    with(subset(fin, Species == "TRAT"), plot(FR.CoYear, Young/Sample))
    
    with(fin[fin$Species == "TRAT",], plot(FR.CoYear, Young / Sample))
    
        plot(fin$FR.CoYear[fin$Species == "BLKI"], 
             fin$Young[fin$Species == "BLKI"]/ fin$Sample[fin$Species == "BLKI"])
    
  • fin$Young
    还必须按
    Species

    原始代码:

    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
    
    改为:

    with(subset(fin, Species == "TRAT"), plot(FR.CoYear, Young/Sample))
    
    with(fin[fin$Species == "TRAT",], plot(FR.CoYear, Young / Sample))
    
        plot(fin$FR.CoYear[fin$Species == "BLKI"], 
             fin$Young[fin$Species == "BLKI"]/ fin$Sample[fin$Species == "BLKI"])
    
  • 如果您愿意学习
    ggplot2
    ,您可以轻松地为每个物种的价值创建单独的图。例如(再一次,如果没有您的数据样本,我无法对此进行测试):

    您可以尝试以下方法:

    基本地块,即两个物种:

    plot(FR.CoYear ~ Young/Sample, data=subset(fin, Species == "TRAT"))
    points(FR.CoYear ~ Young/Sample, col="red",data=subset(fin, Species == "WRAT"))
    
    ggplot(subset(fin, Species %in% c("TRAT", "WRAT")),
           aes(x=FR.CoYear,
           y=Young/Sample,
           color=Species))+
      geom_point()
    
    要添加更多物种,只需添加更多点()

    ggplot2,即对于两个物种:

    plot(FR.CoYear ~ Young/Sample, data=subset(fin, Species == "TRAT"))
    points(FR.CoYear ~ Young/Sample, col="red",data=subset(fin, Species == "WRAT"))
    
    ggplot(subset(fin, Species %in% c("TRAT", "WRAT")),
           aes(x=FR.CoYear,
           y=Young/Sample,
           color=Species))+
      geom_point()
    
    要在这里添加更多物种,只需添加列表c()上的引用即可

    我认为这对您来说是可行的,如果需要,只需测试并更正var名称


    向您致以最诚挚的问候

    以备将来参考,如果您能提供帮助,将更容易为您提供帮助。在本例中,这意味着您的数据样本(除了您已经提供的代码和解释之外)。谢谢您的eipi10,这是一个非常有用的答案,也是ggplot2非常有用的提示。我现在有一些非常好的情节。谢谢