在图中定义填充的2d Contour图的外边缘

时间:2018-01-23 17:04:54

标签: r plotly contour r-plotly

问题:我正在尝试使用plotly在R中重现圆形填充的2d轮廓图(已经尝试了ggplot2但是看起来似乎更容易)。

数据:示例数据下载链接 - https://drive.google.com/file/d/10Mr5yWVReQckPI6TKLY_vzPT8zWiijKl/view?usp=sharing

要为轮廓绘制的数据采用列格式,通常称为z变量,xy数据也适用于z的所有值。一个简单的数据框如下所示:

Sample dataframe

请忽略重复常见的x和y,因为我截断了小数。数据大约有25000行。

方法:我首先使用akima包为给定的zx插入y个变量值,以便在2d中映射z。这使得z列数据适合xy网格,可进行2d绘图并显示轮廓。

预期结果:
2d contour plot of z variable

使用的代码:

dens <- akima::interp(x = dt$`Xvalue(mm)`, 
                  y = dt$`Yvalue(mm)`, 
                  z = dt$Values, 
                  duplicate = "mean",
                  xo=seq(min(dt$`Xvalue(mm)`), max(dt$`Xvalue(mm)`), length = 10),
                  yo=seq(min(dt$`Yvalue(mm)`), max(dt$`Yvalue(mm)`), length = 10))

plot_ly(x = dens$x, 
      y = dens$y, 
      z = dens$z, 
      colors = c("blue","grey","red"), 
      type = "contour")

实际结果: Plotly contour plot

需要帮助: 将实际结果图的边缘细化为与预期结果图像紧密匹配的东西。

非常感谢您的意见和帮助。

1 个答案:

答案 0 :(得分:0)

I found that I could increase the grid output z matrix from akima::interp() from default 40x40 to custom using nx and ny input in function.

And then in plot_ly() add contours = list(coloring = 'fill', showlines = FALSE) to hide contour lines to get output close to my expected outcome.

So working code is like this:

dens <- akima::interp(x = dt$`Xvalue(mm)`, 
              y = dt$`Yvalue(mm)`, 
              z = dt$Values, 
              nx = 50, 
              ny = 50,
              duplicate = "mean",
              xo=seq(min(dt$`Xvalue(mm)`), max(dt$`Xvalue(mm)`), length = 50),
              yo=seq(min(dt$`Yvalue(mm)`), max(dt$`Yvalue(mm)`), length = 50))

plot_ly(x = dens$x, 
  y = dens$y, 
  z = dens$z, 
  colors = c("blue","grey","red"), 
  type = "contour",
  contours = list(coloring = 'fill', showlines = FALSE))

Actual outcome

Plotly contour plot reference was very helpful in this case: https://plot.ly/r/reference/#contour