Reputation: 29
I am trying to create this
but in doing so R says the following:
In plot.xy(xy, type, ...) : NAs introduced by coercion`
I will attach my entire code below:
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)
Here is my data:
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))
Upvotes: 0
Views: 368
Reputation: 1154
Why not use ggplot
?
ggplot(MoleRat, aes(lnmass,lnenergy, color=caste))+geom_point()+
geom_smooth(method='lm',se=FALSE)+ theme_minimal()
Upvotes: 2
Reputation: 7858
You can do it with plot:
plot(lnEnergy ~ lnMass, MoleRat)
points(lnEnergy ~ lnMass, infrequent, col = "red", pch = 19)
points(lnEnergy ~ lnMass, frequent , col = "red")
abline(lm.infrequent)
abline(lm.frequent)
or (easier) with 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)")
However, the image you posted is created by this other model:
lm(lnEnergy ~ lnMass + caste, data = MoleRat)
And based on that, that's the image you will get:
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])
And with 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)")
Upvotes: 0