我有一个名为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我想把FR.CoYear和(fin$Young / fin$Sample)分别画成$Species中的5个物种。
我尝试了建议的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。
发布于 2014-09-26 16:42:08
如果没有您的数据样本,我无法测试下面的答案,但是您的代码中有一些错误,我试图修复这些错误:
with或subset时,您不需要在引用单个列时重新声明数据帧的名称。
原始代码:with(fin,fin$ == == "TRAT"),plot(fin$FR.CoYear,fin$Young /fin$Sample)
改为:
with(fin,物种== "TRAT"),图(FR.CoYear,Young/Sample)plot中重新声明数据帧的名称之外,您还错误地放置了一个括号:
原始代码:
与(鳍鳍$ == "TRAT",,##gives (fin$FR.CoYear,fin$Young / fin$Sample)) ##gives错误:意外的',‘在“with(finfin$ == == "TRAT",plot(fin$FR.CoYear,
改为:
带(鳍鳍$ == "TRAT",图(FR.CoYear,Young / Sample))fin$Young还必须由Species索引。
原始代码:
Fin$FR.CoYearfin$Species == "BLKI",fin$Young /fin$Samplefin$ == "BLKI") xy.coords中的##Error (x,y,x标签,ylabel,log):'x‘和'y’长度不同
改为:
地块(fin$FR.CoYearfin$Species == "BLKI",fin$Youngfin$ == "BLKI"/ fin$Samplefin$ == == "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 ~ .)发布于 2016-09-03 18:06:06
你可以试试这个:
基本地块,即两个物种的地块:
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名称即可。
我最诚挚的问候
https://stackoverflow.com/questions/26064031
复制相似问题