我试图为下面的情节准备一个图例,点缀后跟A,然后是虚线,然后是B.它没有正确显示。由于我有2种美学,它有时只显示一种线型,有时甚至没有。我已使用scale_linetype_manual()
和guides()
,但无法产生预期的效果。但是
我创建的代码,我删除了显示我尝试制作图例的代码。
exemplo<-data.frame(Location = c("Tiburon Peninsula", "San Francisco","Santa Barbara area", "Santa Monica Mountains", "Marin County",
"Santa Cruz Mountains", "Monterey County", "San Diego County","California Coast"),
Area = c(5.9, 45, 110, 320, 529, 1386, 3324,4260, 24520),
Species = c(370L, 640L, 680L, 640L, 1060L, 1200L,1400L, 1450L, 2525L))
modelo<-lm(log(Species,10)~log(Area,10),data=exemplo)
modelo_nls<-nls(Species~a*Area^z,start=list(a=1,z=0.25),data=exemplo)
linha<-data.frame(x=log10(seq(1,25000,100)),y=log10(predict(modelo_nls,newdata=data.frame(Area=seq(1,25000,100)))))
ggplot(data=exemplo,aes(x=log10(Area),y=log10(Species)) ) + geom_point() +
geom_abline(intercept = modelo$coefficients[1], slope = modelo$coefficients[2],linetype="dotted") +
geom_line(data=linha,aes(x=x,y=y),linetype="dashed")+
xlab(label="Área(ha)") + ylab(label="Número de espécies")
答案 0 :(得分:5)
一种方法是将两个预测的行合并为一个数据框,并使用一个额外的列来指示每行所属的模型。
# Add model = 'A' to this data frame (I may have got A and B the wrong way round)
linha <- data.frame(x = log10(seq(1,25000,100)),
y = log10(predict(modelo_nls,newdata=data.frame(Area=seq(1,25000,100)))),
model = 'A')
# Make data frame of line from modelo
lin2 <- data.frame(x = log10(seq(1,25000,100)),
y = predict(modelo, newdata = list(Area = seq(1,25000,100))),
model = 'B')
# Combine two models
combinedLines <- rbind(linha, lin2)
然后用模型绘制作为审美
ggplot(data=exemplo,aes(x=log10(Area),y=log10(Species)) ) + geom_point() +
geom_line(data=combinedLines, aes(x=x,y=y, linetype = model))+
xlab(label="Área(ha)") + ylab(label="Número de espécies")
答案 1 :(得分:1)
这是你如何做到的。
nlha <- data.frame(x=log10(seq(1,25000,100)),y=(predict(modelo,newdata=data.frame(Area=(seq(1,25000,100))))))
model_data <- cbind.data.frame(type=rep(c("A","B"),each=250),rbind(nlha,linha))
ggplot(data=exemplo,aes(x=log10(Area),y=log10(Species)) ) + geom_point() + geom_line(data=model_data,aes(x=x,y=y,linetype=type))+ xlab(label="Área(ha)") + ylab(label="Número de espécies")
我希望它有所帮助。