问题
I'm trying to recreate contour plot shown in the picture below using R. It is a contour plot of constant syrup loss as a function of speed and pressure.

Data used for this example:
speed = c(100,100,100,100,100,100,120,120,120,120,120,120,140,140,140,140,140,140)
pressure = c(10,10,15,15,20,20,10,10,15,15,20,20,10,10,15,15,20,20)
syrup_loss = c(-35,-25,110,75,4,5,-45,-60,-10,30,-40,-30,-40,15,80,54,31,36)
With that data quadratic equation was created:
model <- lm(syrup_loss~speed + pressure + speed^2 + pressure^2 + speed*pressure)
summary(model)
Which gives:
z = 1217.30556 - 31.25625*x + 86.01667*y + 0.1291*x^2 - 2.87333*y^2 + 0.02875*x*y
I tried creating contour plot using this code, but it doesn't give acceptable result:
x = seq(100, 140, len=100)
y = seq( 10, 20, len=100)
z = outer(x, 1217.30556 - 31.25625*x + 86.01667*y + 0.1291*x^2 -
2.87333*y^2 + 0.02875*x*y)
contour(x, y, z, nlev=12)
回答1:
In the outer
function, the Y
argument is missing. The code
z <- outer(x, y, function(x, y) 1217.30556 - 31.25625*x + 86.01667*y + 0.1291*x^2 - 2.87333*y^2 + 0.02875*x*y)
fixed the problem.
来源:https://stackoverflow.com/questions/31328184/contour-plot-for-quadratic-equation-in-r