我的数据看起来像这样
year species number.of.seed dist.to.forest
2006 0 -5
2006 Bridelia_speciosa 3 0
2006 0 5
2006 Carapa 5 10
2006 0 15
我创建了一个条形图,每年显示种子陷阱中发现的不同物种的数量,并显示它们与森林的距离,如下所示:
我想使用geom =“dotplot”,并且有一个点代表我计算的每个物种,基本上与条形图完全相同,但不是2006年的第一个酒吧,24点,而当我使用geom =“dotplot”时,我只是不能让它工作,而不是我想要的方式,我可以用24或23的单点来获得它,但是不是24点。我已经尝试了许多其他类似问题的解决方案,但没有任何工作。非常感谢提前。
我的代码:
dat1<-read.csv(file="clean_06_14_inc_dist1.csv")
diversity<-function(years){
results<-data.frame()
dat2<-subset(dat1, dat1$year %in% years)
for (j in years){
for (i in seq(-5, 50, 5)){
dat3<-subset(dat2, dat2$year == j & dat2$dist.to.forest == i)
a<-length(unique(dat3$species))
new_row<-data.frame(year = j, dist.to.forest = i, number.of.species = a)
results<-rbind(results, new_row)
}}
print(results)
qplot(x = dist.to.forest, y =number.of.species, data = results, facets = .~year, geom = "bar", stat = "identity")
}
diversity(2006:2008)
答案 0 :(得分:1)
我认为你的问题是你正在试图用条形图中的x和y值做一个dotplot-graph,而我相信dotplot-graphs意味着用作直方图,只采用一个变量..
所以,如果我没有弄错的话,如果你在你感兴趣的变量中使你的数据帧不同(因为你想要独特的物种数量),你可以直接绘制它,基本上就像
dat2 = unique(dat1[,c("year","species", "dist.to.forest")])
qplot(x=dist.to.forest, data=dat2, facets=.~year, geom="dotplot")
另一方面,我认为您可能会使这个图表比需要更复杂,您可能需要查看 dplyr ,这使得这种数据操作变得轻而易举。
require(dplyr)
dat1 = tbl_df(read.csv(file="clean_06_14_inc_dist1.csv"))
dat2 = dat1 %.%
filter (year %in% 2006:2008) %.%
group_by (year, dist.to.forest) %.%
summarise (number.of.species = n_distinct(species))
qplot(x=dist.to.forest, y=number.of.species, data=dat2, facets=.~year, geom="bar")
免责声明:由于您未提供任何样本数据,因此此代码不在我的掌控之中,可能包含错误。