Creating Contour Plots in R

I am trying to create contour plots, e.g.

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:

contour(d, add = TRUE)

However, the resulting plot (left) looks quite different from the expected plot (right):

enter image description here

Does anyone know why these two graphs look so different?


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)

DENS <- kde2d(d$a,d$b)

filled.contour(DENS,plot.axes = {
contour(DENS,add = TRUE)})

#> Warning: package 'ggplot2' was built under R version 4.0.5


Created on 2021-07-26 by the reprex package (v0.3.0)

1 Like

Thank you for your reply!

I was thinking of something like this?

#load library

#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))

enter image description here

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.