Reputation: 31
I have included a sample data set just to demonstrate what I am trying to do.
Speed <- c(400,220,490,210,500,270,200,470,480,310,240,490,420,330,280,210,300,470,230,430,460,220,250,200,390)
Hit <- c(0,1,0,1,0,0,1,0,0,1,1,0,0,1,1,1,1,1,0,0,0,1,1,1,0)
obs <- c(1:25)
msl2.data <- as.data.frame(cbind(obs,Hit,Speed))
msl2.glm <- glm(Hit ~ Speed, data = msl2.data, family = binomial)
Doing What I want in the base package.
plot(Hit~ Speed, data = msl2.data, xlim = c(0,700), xlab = "Speed", ylab = "Hit", main = "Plot of hit vs Speed")
pi.hat<-(predict( msl2.glm, data.frame(Speed=c(0:700)), type="response" ))
lines( 0:700, pi.hat, col="blue" )
I am trying to recreate the above plot, but in ggplot
. The error I have been unable to work around is the aes(x,y)
have different lengths, which is true, but I want them to have different lengths.
Any ideas for this in gg?
Upvotes: 3
Views: 5626
Reputation: 25914
You have a couple of approaches; the first does all the modelling
inside of ggplot
, the second does it outside and passes the relevant data
to be plot.
First
gplot(dat=msl2.data, aes(Speed, Hit)) +
geom_point() +
geom_smooth(method="glm", method.args=list(family="binomial"),
fullrange=TRUE, se=FALSE) +
xlim(0, 700)
fullrange
is specified so the prediction lines covers the x-range. xlim
extends the x-axis.
Second
#Create prediction dataframe
pred <- data.frame(Speed=0:700, pi.hat)
ggplot() +
# prediction line
geom_line(data=pred, aes(Speed, pi.hat)) +
# points - note different dataframe is used
geom_point(dat=msl2.data, aes(Speed, Hit))
I generally prefer to do the modelling outside (second approach), and use ggplot
purely as a plotting mechanism.
Upvotes: 6