Home > Software design >  R: Contour Plots for Functions
R: Contour Plots for Functions

Time:03-10

I am working with the R programming language.

I am trying to make a Contour Plot for the following function:

library(plotly)


  my_function <- function(x,y) {
    
    final_value = x^2   y^2
    }


input_1 <- seq(-1.5, 1.5,0.1)
input_2 <- seq(-1.5, 1.5,0.1)


z <- outer(input_1, input_2, my_function)

plot_ly(x = input_1, y = input_2, z = z) %>% add_surface()

enter image description here

I am trying to follow the instructions from this link over here: enter image description here

Can someone please tell me if what I have done is correct? Is this how Contour Plots (Level Plots) are made for functions using ggplot2?

The reason I am skeptical is because I tried to repeat this process for a completely different function (enter image description here

z <- outer(x1, x2, my_function)

plot_ly(x = x1, y = x2, z = z) %>% add_surface()

enter image description here

I don't think this is correct - both functions look entirely different and I doubt they would have the same contour plots. This makes me skeptical and think that I am doing something wrong.

Thanks!

Note: Ideally, the contour plot of the second function should look something like this: enter image description here

  • Related