1
votes

I'm trying to create a contour plot in R from 3 vectors.

I have:

x=c(1,1,1,2,2,2,3,3,3)
y=c(0,10,20,0,10,20,0,10,20)
z=c(900,800,700,600,500,400,300,200,100)

And I want to plot something like that (made in SigmaPlot) enter image description here

How can I do it in R?

1
Have you tried to write code for this? If so, you should post it, and show where you are stuck. The package ggplot2 and geom_contour() would be a starting point: e.g., ggplot(df, aes(x = x, y = y, z = z)) + geom_contour() From there, you would change the fill, add a legend and labeling. - ssp3nc3r
Or you could look at the base graphics function contour - G5W

1 Answers

6
votes

You can use plotly package:

x=c(1,1,1,2,2,2,3,3,3)
y=c(0,10,20,0,10,20,0,10,20)
z=c(900,800,700,600,500,400,300,200,100)
df <- data.frame(x=x,y=y,z=z)

library(plotly)    
p <- plot_ly(data = df, x=~x,y=~y, z=~z, type = "contour", colorscale='Jet')

This will give you:

> p

enter image description here