绘图问题,R编程

时间:2018-12-24 12:16:32

标签: r plotly contour r-plotly

我有一个矩阵,其y轴上的NA值位于“ 0.75”点,但是当绘制等高线图时,图形的空白区域出现在“ 10”和“ 10.75”处点。

我很确定这是因为我在轴上缩放(我已经使用中的指定方程式完成了缩放),但不知道我要去哪里。 图中的其他空白区域是正确的,并且是NA值。

这是数据帧WW3_MF

   V1   V2   V3   V4   V5   V6   V7   V8   V9  V10
   NA -1.5 -1.0 -1.0   NA  0.5 -1.0 -1.0 -1.5 -3.0
   NA -1.5 -1.0 -0.5  0.0  1.0 -1.0 -1.0 -2.0 -4.0
   NA -1.5 -1.0 -1.0  1.0   NA  0.0 -1.0 -2.5 -4.5
   NA -1.5 -1.0 -1.0   NA  1.5   NA -1.0 -3.0 -4.5
   NA -1.0 -1.0 -0.5  1.0  1.5 -1.0 -1.0 -2.5 -4.5
   NA -1.5 -2.0 -0.5  1.0  2.0 -2.0 -1.0 -3.0 -5.0
   NA -1.5 -2.0 -0.5  0.5  1.5 -2.0 -1.5 -3.0 -4.5
   NA -1.5 -2.0 -1.0 -0.5  1.5 -2.5 -1.0 -3.0 -4.5
   NA -0.5 -1.0   NA  1.0   NA -3.5 -1.0 -3.0 -4.5
   NA -0.5 -1.5 -1.0  0.0  2.5 -3.5  0.0 -3.0 -4.5
   NA -1.0 -1.0 -0.5 -0.5  2.5   NA -0.5 -3.0 -5.5
   NA -0.5 -1.5  0.5 -1.0  3.0 -4.0  0.5 -2.5 -5.0
   NA -0.5 -1.0 -0.5 -1.5  3.0 -4.5  0.5 -2.5 -5.0
   NA -0.5 -1.5 -0.5   NA  2.5 -3.5  0.0 -2.5 -4.5
   NA  0.0 -1.0   NA -2.0  3.0 -2.5 -0.5 -2.5 -4.0
   NA  0.0 -1.0 -1.0 -1.5  3.5 -3.0  0.5 -3.0 -4.0
   NA  0.5 -0.5 -1.5   NA  2.5 -2.0  0.5 -2.5 -4.0
   NA  0.5  0.5  0.5 -1.0  2.5 -1.0  1.0 -2.0 -4.0
   NA  0.5  0.5  0.5 -1.0  3.0 -1.0  1.0 -2.0 -3.5
   NA -1.0  0.5  0.0  0.5  2.0 -0.5  0.5 -2.0 -3.5
   NA  0.5  1.0  0.5  0.5  2.0  0.5  0.5 -1.5 -3.5
   NA  0.5  0.5 -0.5  0.0  1.5  0.5  1.0 -1.5 -3.0
   NA  0.5  0.5 -0.5   NA  1.0  0.5  0.0 -1.0 -3.0
   NA  0.5  0.0 -1.5 -1.0 -0.5   NA -1.0 -2.0 -3.0
   NA  1.0  0.5 -1.0 -1.0  0.5 -0.5 -1.0 -2.0 -2.5
   NA  1.0 -0.5 -1.0 -1.0  0.0  0.5 -1.5 -1.5 -3.0
   NA  1.0  0.0 -1.0 -1.0 -1.0 -1.0 -1.5 -2.0 -3.0
   NA  1.5  1.0 -1.0 -1.0 -0.5 -1.0 -1.5 -2.0 -3.0
   NA  0.5  0.5 -1.0 -1.5 -1.0 -2.0 -2.0 -3.0 -3.5
   NA  0.5  0.5 -1.0 -1.5 -1.0 -2.0 -2.0 -3.0 -3.0
   NA  0.0 -0.5 -1.0 -2.0 -1.5 -2.5 -2.0 -3.5 -3.0
   NA -0.5 -0.5 -1.5 -2.0 -1.5 -2.5 -2.0 -4.0 -3.0
   NA -0.5 -0.5 -1.5 -2.0 -1.5 -2.5 -2.5 -4.0 -3.5
   NA -0.5 -1.0 -1.5 -2.0 -1.5 -2.5 -2.5 -4.0 -3.5
   NA  0.5 -0.5 -1.0 -1.5 -1.0 -2.0 -2.0 -4.0 -3.0
   NA  0.5 -1.0 -1.0 -1.5 -1.5 -3.0 -2.0 -5.0 -3.0
   NA  0.5  0.0 -1.0 -1.0 -1.0 -2.5 -2.0 -5.0 -3.0
   NA  0.5  0.5 -1.0 -1.5 -1.5 -3.0 -2.0 -5.0 -3.0
   NA  0.5 -0.5 -1.0 -1.0 -1.5 -3.5 -1.5 -4.5 -3.0
   NA  1.0  0.5 -1.0 -1.0 -2.5 -3.0 -2.0 -4.5 -3.5
   NA  1.5 -0.5 -1.5 -1.0 -2.0 -3.0 -1.5 -3.5 -3.5
   NA  1.5 -1.0 -1.0 -1.0 -2.0 -3.0 -1.0 -3.0 -3.5
   NA  1.0 -1.0 -1.0 -1.5 -2.5 -3.0 -1.0 -3.0 -3.0
   NA  1.5 -0.5 -1.0 -1.0 -3.0 -2.5 -1.0 -2.5 -3.0
   NA  1.0 -1.5 -1.0 -1.5 -3.0 -3.0 -1.5 -4.0 -2.5

这是我的R脚本。

p <- plot_ly(z = matrix(c(WW3_MF)),  type="contour", width=500,   height=1074, colorbar = list(title = "Accumulation/Erosion (cm)"), 
         colorscale=list (c(0, "rgb(0,0,255)"),list(0.65, "rgb(135,206,250)"), list(0.75, "rgb(255,255,0)"), list(0.85, "rgb(255,165,0)"), list(1, "rgb(255,0,0)")),zauto=F, zmin=-14, zmax=8) %>%
layout(title="WW3_MF", 

yaxis=list(title="Y(m)", tickmode = "array",ticktext = c("-0.75", "-0.1", "0.1", "0.5","1.5", "2.5", "5", "7.5", "10", "10.75"),tickvals = c("1", "1.51", "1.67", "1.98","2.76", "3.54", "5.5", "7.46", "9.41", "10"),range = c(1,10)), 

xaxis=list(nticks="4",title="X(m)",tickmode = "array",ticktext = c("1", "2", "3"),tickvals = c("15", "30", "45"),range=0,45), shapes =list(type = "rect",line = list(color = "black"), x0 = "0", x1 = "22.5", xref = "x",y0 = "0.58", y1 = "8.41", yref = "y"))
(contours = list(z =list(show=TRUE, usecolormap=TRUE,highlightcolor="#ffA500",project=list,countourlabels="TRUE")))
    (scene = list(camera=list(eye = list(x=1.87, y=0.88, z=-0.64))))p

这里是contour plot

WW3_MF, contour plot

这是范围为(0,9),tickvals = c(“ 0”,“ 0.509”,“ 0.665”,“ 0.978”,“ 1.761”,“ 2.543”,“ 4.5”, “ 6.457”,“ 8.413”,“ 9”)

enter image description here

1 个答案:

答案 0 :(得分:0)

我不确定100%是否能正确理解您的问题,但这是一个猜测。

在图形上方看到的空白不是来自NA值,而是因为数据框中没有“ no”值。 您可以将y轴的range指定为1到10,但是数据实际上从0开始到9。

如果设置range = c(0, 9),则应正确绘制数据。尝试更改第一行中的某些NA,您会发现有所不同。

此外,tickvals作为字符串数组而不是数字/浮点数提供。由于您没有线性比例尺,因此Plotly“ mis”会解释基础数据。尝试删除数字周围的刻度,数字看起来会更好。