drawing a multi variable linear line using ggplot2

658 views
Skip to first unread message

Mike Dylan

unread,
Jan 3, 2013, 3:30:48 PM1/3/13
to ggp...@googlegroups.com

I need to create a multivariable regression line using ggplot.

My data:

    dput(head(x2,15))
    structure(list(Date = structure(c(15608, 15609, 15610, 15611,
    15612, 15613, 15614, 15615, 15616, 15617, 15618, 15619, 15620,
    15621, 15622), class = "Date"), Cpu = c(77.0763, 51.8909, 59.3229,
    89.5822, 87.7448, 80.4413, 57.5009, 99.8185, 99.9969, 91.5528,
    50.0793, 56.4049, 57.808, 51.0453, 56.0505), Memory = c(369.667979452055,
    341.572253381722, 345.013066490241, 334.520135424091, 374.107056613899,
    1592.38342810723, 470.204599904169, 393.802909594735, 540.817571059432,
    425.49563812601, 438.326775174387, 614.417456359102, 1255.63550519358,
    466.993243243243, 358.445879354291), Response = c(52.25, 48.36,
    49.23, 50.99, 48.63, 46.11, 43.03, 45.35, 50.03, 46.18, 47.39,
    43.28, 55.36, 50.59, 50.44)), .Names = c("Date", "Cpu", "Memory",
    "Response"), row.names = c(1L, 4L, 6L, 7L, 9L, 10L, 13L, 16L,
    19L, 25L, 29L, 32L, 35L, 39L, 42L), class = "data.frame")

I need to draw a linear line with this:  `Response ~ Cpu +Memory

    ggplot(x2, aes(Response)) +
       geom_point(aes(y = Memory), size = 2, colour = "blue") +
       geom_point(aes(y = Cpu), size = 2, colour = "orange") +
       geom_smooth(method = "lm", formula = "Response ~ CPU+Memory",
                   size = 1.5, colour = "red", se = T)

I am getting this error:

    Error: stat_smooth requires the following missing aesthetics: y

Any ideas?

 

Dennis Murphy

unread,
Jan 3, 2013, 3:47:43 PM1/3/13
to Mike Dylan, ggp...@googlegroups.com
Hi:

No can do. geom_smooth() only plots 2D lines/curves, not
planes/surfaces in (pseudo-) 3D. Moreover, the formula has the form y
~ f(x), by which I mean that the formula used must include y as the
response and x as the covariate. One possibility is to try the
scatterplot3d package - its vignette includes an example of how to
plot a plane in a pseudo-3D space.

Dennis
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Peter Ellis (Wellington)

unread,
Jan 6, 2013, 10:21:59 PM1/6/13
to Dennis Murphy, Mike Dylan, ggp...@googlegroups.com
I agree you may want to look at alternatives for 3d plotting, but are you perhaps after something as simple as the below. This is a different interpretation of your question but looking at your code this might be one aspect of what you were heading for. It doesn't show the model you ask for, but two marginal models instead. My interpretation is based on the fact that you asked for a "line", and your snippet of code as far as geom_point() is more in tune with what I have below than a three-d picture.

You had a fundamental problem in that you had both of your explanatory variables on the same axis even though they were very different scales, so I have used facets to separate them out. I have also put them on the horizontal axis which is the usual presentation. And I have connected together your points (so it becomes a connected scatter plot) to indicate the time series nature of your data

x2.m <- melt(x2, id.vars=c("Date", "Response"))

ggplot(x2.m, aes(x=value, y=Response, color=variable)) +
geom_point(size=2) +
geom_line() +
geom_smooth(method="lm", size=2, lty=2) +
facet_wrap(~variable, scales="free_x")


PE

Peter Ellis 
newzealand.govt.nz - connecting you to New Zealand central & local government services

Any opinions expressed in this message are not necessarily those of the Ministry of Business, Innovation and Employment. This message and any files transmitted with it are confidential and solely for the use of the intended recipient. If you are not the intended recipient or the person responsible for delivery to the intended recipient, be advised that you have received this message in error and that any use is strictly prohibited. Please contact the sender and delete the message and any attachment from your computer.
cpu and memory v response.png
Reply all
Reply to author
Forward
0 new messages