我想绘制4种处理中6种动物的平均访问率。我希望每个物种都有一条反映速率的线。当我运行此代码时:
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
抱歉,我不知道如何在这里编写表格。
错误是因为有太多的零?
答案 0 :(得分:0)
我不得不删除lm,我认为你真的不能用当前的数据来做。数据文件只是.csv格式的数据......
library(plotly)
library(RColorBrewer)
#library(extrafont)
library(MASS)
#library(lme4)
library(reshape2)
melt.data<-data.frame(read.csv(file="C:\\TEMP\\testdata.csv"))
data.tall<- melt(melt.data, id.vars=c("Treatment"))
data.tall$variable <- as.factor(data.tall$variable)
plot1<- ggplot(data.tall, aes(x = Treatment, y = value)) +
geom_point(aes(colour = factor(variable))) +
facet_grid(Treatment~variable, margins=FALSE,scales="free_y")+
# stat_smooth(method = 'lm',aes(color = variable),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(plot1)