[R] contourplot
David L Carlson
dcarlson at tamu.edu
Fri Mar 22 17:21:11 CET 2013
Look carefully at the help file. Your x1 and x2 need to define a rectangular
grid so that there is a value of y for each combination of different x1 and
x2 values. When this is not the case, you don't get an error, just a blank
plot. It is not clear from the little piece of data that you included if
that is the case, but the blank plot suggests that you don't have a grid.
You can created gridded data using expand.grid and predict.lm() to generate
estimates of y for every combination of x1 and x2:
> # Reproducible data
> set.seed(42)
> x1 <- rnorm(100, 60, 10)
> x2 <- rnorm(100, 37, 5)
> y <- x1+x2
> pr <- data.frame(x1, x2, y)
> # Create grid that spans x1/x2 ranges
> # 50 values each ranging from min to max
> xg <- seq(min(x1), max(x2), length.out=50)
> yg <- seq(min(x2), max(x2), length.out=50)
> # Grid will have 50 x 50 = 2500 values
> xygrid <- expand.grid(xg, yg)
> # Create data frame from gridded data and compute y
> # from a linear regression of x1 and x2 on y
> lmmodel <- lm(y~x1+x2, pr)
> prgrid <- data.frame(x1=xygrid$Var1, x2=xygrid$Var2)
> prgrid$y <- predict(lmmodel, prgrid)
> # Draw contour plot
> contourplot(y~x1+x2, prgrid)
----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Pascal Oettli
> Sent: Thursday, March 21, 2013 11:41 PM
> To: Steven LeBlanc
> Cc: r-help at r-project.org
> Subject: Re: [R] contourplot
>
> Hi,
>
> What is the result of:
>
> contourplot(Y~X1+X2,data=pr2)
>
> HTH,
> Pascal
>
>
> Le 13/03/22 8:57, Steven LeBlanc a écrit :
> > Greets,
> >
> > I'm using a data frame that looks like:
> >> head(pr2)
> > X1 X2 X3 X4 Y fit res
> > 1 44 33.2 5 30 41.2 39.22201 1.977991
> > 2 43 33.8 4 41 31.7 38.48476 -6.784761
> > 3 48 40.6 3 38 39.4 44.78278 -5.382783
> > 4 52 39.2 7 48 57.5 51.48134 6.018656
> > 5 71 45.5 11 53 74.8 68.25585 6.544153
> > 6 44 37.5 9 65 59.8 53.27743 6.522569
> >
> > Along with the command:
> >> contourplot(Y~X1*X2,data=pr2)
> >
> > But I get a blank plot. I thought it might be because the data were
> unsorted or sparse, so I made another ordered data frame as follows:
> >
> >> head(new)
> > X1 X2 X3 X4 resp
> > 1 1 1 1 1 -10.810406
> > 2 2 2 2 2 -7.657712
> > 3 3 3 3 3 -4.505018
> > 4 4 4 4 4 -1.352323
> > 5 5 5 5 5 1.800371
> > 6 6 6 6 6 4.953065
> >
> > But the result is the same. Any idea why this does not work?
> >
> > Best Regards,
> > Steven
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list