简体   繁体   中英

Basic x, y, z plot using CSV in R

I am new to using R, and I am trying make a simple groundwater contour plot with some dummy data:

library("readr")
library("plotly")

#Load data from CSV file, preview data.
mock_gw_data <- read.csv(file = '/cloud/project/mock_gw_data.csv')
#View(mock_gw_data)

fig <- plot_ly(
  x = c(mock_gw_data$x), 
  y = c(mock_gw_data$y), 
  z = matrix(c(mock_gw_data$z)), 
  type = "contour"
  )

fig

With the following data:

x,y,z
4,4,0.2
3,2,0.21
1,0,0.21
4,-1,0.2
3,-2,0.19
2,-4,0.18
-4,-4,0.21
-2,-3,0.21
-1,-1,0.22
-4,1,0.23
-3,4,0.22
-1,2,0.22

But it basically doesn't work. I am unclear about how I should be handling the data, and the standard contour function doesn't work with this, as it contains unordered coordinates. Basically I just want to plot a contour of the Z coordinate.

Any advice appreciated!

Try this:

library("readr")
library("plotly")

#Load data from CSV file, preview data.
mock_gw_data <- read.csv(file = 'for_plot.csv', sep=',') #add a type of sep in your csv file 
#View(mock_gw_data)

fig <- plot_ly(
    x = mock_gw_data$x, 
    y = mock_gw_data$y, 
    z = matrix(mock_gw_data$z, nrow = 4, ncol = 3), #matrix should have rows and cols, don't forget it
    type = "contour"
)

fig

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM