# Creating Contour Plots in R

I am trying to create contour plots, e.g. https://i.stack.imgur.com/C31CQ.png

I created some data for this example:

``````#generate data
a <- rnorm(100,10,10)
b <- rnorm(100,5,5)
c <- rnorm(100,1,1)
d - data.frame(a,b,c)
d = as.matrix(d)
``````

Then, I tried to run the code to make the contour plots:

``````image(d)
``````

However, the resulting plot (left) looks quite different from the expected plot (right): Does anyone know why these two graphs look so different?

Thanks

I am not sure what you intend to do with the third variable.

``````set.seed(345) # make the example reproducible
a <- rnorm(100,10,10)
b <- rnorm(100,5,5)
c <- rnorm(100,1,1)
d <- data.frame(a,b,c)

library(MASS)
DENS <- kde2d(d\$a,d\$b)
contour(DENS)
`````` ``````
filled.contour(DENS,plot.axes = {
axis(1)
axis(2)

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.5
`````` ``````ggplot(d,aes(x=a,y=b))+geom_density2d()
`````` Created on 2021-07-26 by the reprex package (v0.3.0)

1 Like

I was thinking of something like this?

``````#load library
library(akima)

#create plot

fld <- with(DF, interp(x = X1, y = X2, z = total))

filled.contour(x = fld\$x,
y = fld\$y,
z = fld\$z,
color.palette =
colorRampPalette(c("white", "blue")),
xlab = "X1",
ylab = "X2",
main = "Contour Plot",
key.title = title(main = "total ", cex.main = 1))
`````` This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.