【问题标题】:Plotting a subset of a dataframe with R?用 R 绘制数据框的子集?
【发布时间】:2014-11-21 17:29:26
【问题描述】:

我有一个名为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)

我尝试了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。

【问题讨论】:

  • 为了将来参考,如果您提供reproducible example,会更容易帮助您。在这种情况下,这意味着您的数据样本(除了您已经提供的代码和解释)。

标签: r syntax plot subset


【解决方案1】:

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

  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,您可以轻松地为 Species 的每个值创建单独的图。例如(再一次,如果没有您的数据样本,我无法对此进行测试):

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,这是一个非常有用的答案,对 ggplot2 非常有用。我现在有一些非常好的情节。谢谢。
【解决方案2】:

你可以试试这个:

基本情节,即两个物种:

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

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

ggplot2,即两个物种:

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

要在此处添加更多物种,只需在列表 c() 中添加引用。

我认为这对你有用,如果需要,只需测试和更正 var 名称。

致以最诚挚的问候

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 2018-11-24
    相关资源
    最近更新 更多