【发布时间】:2016-10-31 11:12:06
【问题描述】:
我想绘制 6 种动物在 4 次治疗中的平均访问率。我希望每个物种都有一条反映速率的线。当我运行这段代码时:
library(plotly)
library(RColorBrewer)
library(extrafont)
library(MASS)
library(lme4)
library(reshape2)
dat<-read.table("R-1.AverageVisitationRatewithTrappingPeriod.csv",header=T,sep=",")
melt.data <- melt(dat,id = c("Site", "Treatment", "TrappingPeriod", "Rainfall"),
variable.name="Species",value.name="AverageVisitationRate", na.rm = TRUE)
melt.data$Treatment<-as.factor(melt.data$Treatment)
Treatment<-melt.data$Treatment
Species<-melt.data$Species
AverageVisitationRate<-melt.data$AverageVisitationRate
str(melt.data)
melt.data
plot<- ggplot(melt.data, aes(x = Treatment, y = AverageVisitationRate)) +
geom_point(aes(color = Species)) +
facet_grid(Treatment~Species, margins=FALSE,scales="free_y")+
stat_smooth(method = 'lm',aes(color = Species),se = FALSE)+
labs(list(x = "Treatment", y = "Average Visitation Rate"))+
theme(axis.title = element_text(family = "Arial", color="black", size=20),strip.background = element_blank())
ggplotly(plot)
然后它会产生这个错误:
Error in $<-.data.frame(tmp, "alpha", value = 1) : replacement has 1 row, data has 0
这是我的数据示例:
Treatment Buffalo Impala Nyala Warthog WhiteRhino Wildebeest Zebra
1 - 0% 0 0 0 0 0 0 0
1 - 0% 0.166666667 0.25 0 0.083333333 0 0 0
1 - 0% 0 0.5 0 0 0 0 0
1 - 0% 0 0 0.125 0 0 0 0
1 - 0% 0 0 0.038461538 0.076923077 0.076923077 0 0.038461538
2 - 5% 0.5 0.357142857 0.5 0 0.142857143 0 0
2 - 5% 0 0.25 0.25 0.083333333 0.166666667 0 0
2 - 5% 0 0 0.0625 0 0 0 0
2 - 5% 0 0.2 0 0 0 0 0
2 - 5% 0 0.076923077 0.038461538 0 0.038461538 0 0.153846154
3 - 10% 0.625 0.375 0.5 0.3125 0.5625 0 0.0625
3 - 10% 1.833333333 1.416666667 0.166666667 0.333333333 0 0 0
3 - 10% 0 0 0 0 0 0 0
3 - 10% 0.25 0 0.125 0 0 0 0
3 - 10% 0 1.2 0 0.4 0 0 0
4 - 20% 0 0 0 0 0 0 0
4 - 20% 0.333333333 0.5 0.083333333 0.083333333 0.083333333 0 0
4 - 20% 0 0.1875 0.25 0.125 0 0 0
4 - 20% 0 0.25 0 0 0 0 0
4 - 20% 0.115384615 0.076923077 0.269230769 0.076923077 0.115384615 0 0.115384615
抱歉,我不知道如何在这里编写表格。
错误是因为有太多的零吗?
【问题讨论】:
-
您可以使用
dput(myData)提供您的数据。请阅读并关注how to provide minimal reproducible examples in R,以方便其他人提供帮助。您的示例应该可以复制粘贴运行,直到出现错误。 -
您的示例数据中缺少列? melt.data