0

我正在尝试创建这个

在此处输入图像描述

但在这样做时,R 会说以下内容:

在 plot.xy(xy, type, ...) 中:强制引入的 NAs`

我将在下面附上我的整个代码:

lnmass <- MoleRat$lnMass
lnenergy <- MoleRat$lnEnergy
caste <- MoleRat$caste

infrequent <- MoleRat[caste == "lazy", ]
frequent <- MoleRat[caste == "worker", ]

lm.infrequent <- lm(lnEnergy ~ lnMass, data = infrequent) #, subset=caste=="lazy")
lm.frequent   <- lm(lnEnergy ~ lnMass, data = frequent)

plot(lnmass, lnenergy, pch = as.numeric(caste), col = as.numeric(caste))
abline(lm.infrequent)
abline(lm.frequent)

这是我的数据:

dput(MoleRat)
structure(list(caste = c("worker", "worker", "worker", "worker", 
"worker", "worker", "worker", "worker", "worker", "worker", "worker", 
"worker", "worker", "worker", "worker", "worker", "worker", "worker", 
"worker", "worker", "worker", "lazy", "lazy", "lazy", "lazy", 
"lazy", "lazy", "lazy", "lazy", "lazy", "lazy", "lazy", "lazy", 
"lazy", "lazy"), lnMass = c(3.850147602, 3.988984047, 4.110873864, 
4.17438727, 4.248495242, 4.262679877, 4.343805422, 4.48863637, 
4.510859507, 3.951243719, 3.988984047, 4.158883083, 4.234106505, 
4.276666119, 4.248495242, 4.465908119, 4.532599493, 4.510859507, 
4.828313737, 4.753590191, 4.875197323, 4.382026635, 4.543294782, 
4.912654886, 4.700480366, 4.700480366, 4.762173935, 4.859812404, 
5.056245805, 5.262690189, 5.147494477, 5.087596335, 4.997212274, 
4.875197323, 4.812184355), lnEnergy = c(3.688879454, 3.688879454, 
3.688879454, 3.663561646, 3.871201011, 3.850147602, 3.931825633, 
3.688879454, 3.951243719, 4.110873864, 4.189654742, 4.143134726, 
4.262679877, 4.248495242, 4.510859507, 4.394449155, 4.219507705, 
4.48863637, 4.644390899, 5.017279837, 5.043425117, 3.828641396, 
4.143134726, 3.555348061, 4.060443011, 4.094344562, 4.304065093, 
4.094344562, 4.418840608, 4.234106505, 4.49980967, 4.574710979, 
4.532599493, 4.615120517, 4.48863637)), class = "data.frame", row.names = c(NA, 
-35L))
4

2 回答 2

2

为什么不使用ggplot

ggplot(MoleRat, aes(lnmass,lnenergy, color=caste))+geom_point()+
geom_smooth(method='lm',se=FALSE)+ theme_minimal()
于 2020-11-27T19:23:37.950 回答
0

你可以用情节做到这一点:

plot(lnEnergy ~ lnMass, MoleRat)
points(lnEnergy ~ lnMass, infrequent, col = "red", pch = 19)
points(lnEnergy ~ lnMass, frequent  , col = "red")
abline(lm.infrequent)
abline(lm.frequent)

在此处输入图像描述

或(更容易)使用ggplot:

library(ggplot2)

ggplot(MoleRat, aes(x = lnMass, y = lnEnergy, colour = caste)) +
 geom_point(size = 2) +
 geom_smooth(formula = y~x, method = "lm", se = FALSE) +
 theme_classic() +
 labs(x = "ln(body mass)",
      y = "ln(daily energy expenditure)")

在此处输入图像描述


但是,您发布的图像是由这个其他模型创建的:

lm(lnEnergy ~ lnMass + caste, data = MoleRat)

基于此,这就是您将获得的图像:

lm.graph <- lm(lnEnergy ~ lnMass + caste, data = MoleRat)

plot(lnEnergy ~ lnMass, MoleRat)
points(lnEnergy ~ lnMass, infrequent, col = "red", pch = 19)
points(lnEnergy ~ lnMass, frequent  , col = "red")

lmcoef <- coef(lm.graph)
abline(a = lmcoef[1], b = lmcoef[2])
abline(a = lmcoef[1] + lmcoef[3], b = lmcoef[2])

在此处输入图像描述

并使用ggplot:

MoleRat$prd <- predict(lm.graph, MoleRat)

ggplot(MoleRat, aes(x = lnMass, colour = caste)) +
 geom_point(aes(y = lnEnergy), size = 2) +
 geom_line(aes(y = prd), size = 1) +
 theme_classic() +
 labs(x = "ln(body mass)",
      y = "ln(daily energy expenditure)")

在此处输入图像描述

于 2020-11-27T19:29:03.990 回答