gpt4 book ai didi

r - ggplot2 中两种美学和 geom_abline 的图例

转载 作者:行者123 更新时间:2023-12-02 08:20:45 25 4
gpt4 key购买 nike

我正在尝试为以下情节准备图例,虚线后跟 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")

最佳答案

一种方法是将您的两条预测线组合成一个数据框,并用一个额外的列指示每行属于哪个模型。

# 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")

enter image description here

关于r - ggplot2 中两种美学和 geom_abline 的图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37547404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com