简体   繁体   中英

Error in ggplotly

I want to graph the average visitation rate of 6 species of animals over 4 treatments. I want each species to have a line reflecting the rate. When I runs this code:

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)

It then produces this error: Error in $<-.data.frame( tmp , "alpha", value = 1) : replacement has 1 row, data has 0

Here is a sample of my data:

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

Sorry i'm not sure how to code a table in here.

Is the error as a result of having too many zeros?

I had to remove the lm, I don't think you really can do it with the current data. The data file is just your data in .csv format...

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)

举例说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM