문제

I have begun switching from base R graphics to lattice graphics. I now have some data and an nls() fit to that data, and am I'm trying to plot both onto a single graph. So, as a simple base R graphics code:

require(lattice)
# Data
V <- c(45.5513,57.9145,66.3092,72.3338,79.7007,92.5738,107.9735,153.7357,210.8857)
E <- c(-0.05403,-0.20890,-0.24562,-0.25644,-0.26019,-0.25213,-0.23181,-0.17106,-0.10860)

# Guesses
V0g <- 118.5614
E0g <- -0.2579124
B0g <- 0.004868107
Bp0g <- 4

# nls fit
birchfit <- nls(E ~ E0 + (9*V0*B0)/16 * ( ((V0/V)^(2/3)-1)^3*Bp0 + ((V0/V)^(2/3)-1)^2 * (6-4*(V0/V)^(2/3))  ),start=list(B0=B0g,V0=V0g,Bp0=Bp0g,E0=E0g))

# Plot
VV <- seq(V[1],tail(V,1),length=1000)
plot(V,E)
lines(VV,predict(birchfit,data.frame(V=VV)))

Now for the lattice variant, I try this,

VV <- seq(V[1],tail(V,1),length=1000)
print(xyplot(E ~ V,
     panel=panel.xyplot(VV,predict(birchfit,data.frame(V=VV)),type="l")))

Then I am told (by text in the center of the graph)

Error using packet 1
'what' must be a character string or function
도움이 되었습니까?

해결책

I was able to achieve what I was looking for by using the as.layer from latticeExtra package to add two separate graphs to the same panel. Maybe there's a better solution, but this worked for me.

require(latticeExtra)
...
VV <- seq(V[1],tail(V,1),length=1000)
plot1 <- xyplot(E ~ V)
plot2 <- xyplot(predict(birchfit,data.frame(V=VV)) ~ VV,type="l")
print(plot1 + as.layer(plot2))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top