R 中唯一的错误。default(x) : unique() applies only to vectors

标签 r plotly

我是 R 新手,也是堆栈溢出新手,如果我犯了错误,或者这个问题很容易解决,我很抱歉。

我正在为一些数据创建 3D 散点图,但不断收到错误: unique.default(x) 中的错误:运行此代码块时,unique() 仅适用于向量。

我查看了其他有关该错误的帖子,但没有一个对我有帮助。

plot1 <- data.frame(Power = as.vector(colMeans(simElec)), gas = as.vector(colMeans(simNG)), margin = as.vector(subset(profitPlot, segment == "Total" & external == 'External Only')$profit))
    plot_ly(plot1, x = ~Power, y = ~gas, z = ~margin, color = ~am, 
            marker = list(color = ~margin, 
                          colorscale = list(c(0, "rgb(227, 25, 54)"), list(1, "rgb(194, 205, 35)")),
                          showscale = F)) %>%
      add_markers() %>%

   # > plot1
   #      Power      gas   margin
   #1  69.98741 1.178873 188.1146
   #2  52.88238 1.491435 160.4246
   #3  57.46660 1.213390 167.1698
   #4  54.76827 1.186165 166.2039
   #5  61.80076 1.853139 170.7245
   #6  67.20337 1.199860 175.0212
   #7  55.86961 1.337983 161.5914
   #8  64.59154 1.420715 166.0721
   #9  63.66839 1.966030 175.0758
   #10 77.92848 1.888605 190.7074
   #11 47.92403 1.671808 142.5698
   #12 70.65457 1.354181 174.6854
   #13 63.62497 1.665640 170.6355
   #14 69.06129 1.144999 184.9754
   #15 56.77372 1.272378 161.6641

      layout(
          title = "Test",
          scene = list(xaxis = list(title = 'Electricity ($/MWh)'),
                          yaxis = list(title = 'Gas ($/GJ)'),
                          zaxis = list(title = 'Commodity Margin ($ million)'))
          )

我知道我已经很接近了,但我不知道出了什么问题。

> dput(plot1)
structure(list(Power = c(69.9874083088991, 52.8823845511674, 
57.466598971878, 54.7682734834554, 61.8007644271192, 67.2033748459662, 
55.8696111041597, 64.5915381203366, 63.6683866870196, 77.9284830962032, 
47.9240347517641, 70.6545718514277, 63.624972518279, 69.0612933117757, 
56.7737161200819), gas = c(1.1788726505072, 1.49143516242048, 
1.21339029609897, 1.18616542299913, 1.85313915518133, 1.19986033487848, 
1.33798310271697, 1.42071527189892, 1.96603005333785, 1.88860512508931, 
1.67180843784324, 1.35418148197551, 1.6656400541228, 1.14499850347505, 
1.27237787462861), margin = c(188.114554495799, 160.424593244341, 
167.169831096674, 166.203918308567, 170.724537947621, 175.021242542284, 
161.591428698362, 166.072058276843, 175.075797678045, 190.707360569274, 
142.569752983863, 174.685436684351, 170.63549056998, 184.975371979721, 
161.664095244404)), class = "data.frame", row.names = c(NA, -15L
))

最佳答案

从对 plot_ly 函数的调用中删除 color = ~am 参数似乎可以生成所需的绘图。下面的代码和结果图。

library(plotly)

plot1 <- structure(list(Power = c(69.9874083088991, 52.8823845511674,
57.466598971878, 54.7682734834554, 61.8007644271192, 67.2033748459662,
55.8696111041597, 64.5915381203366, 63.6683866870196, 77.9284830962032,
47.9240347517641, 70.6545718514277, 63.624972518279, 69.0612933117757,
56.7737161200819), gas = c(1.1788726505072, 1.49143516242048,
1.21339029609897, 1.18616542299913, 1.85313915518133, 1.19986033487848,
1.33798310271697, 1.42071527189892, 1.96603005333785, 1.88860512508931,
1.67180843784324, 1.35418148197551, 1.6656400541228, 1.14499850347505,
1.27237787462861), margin = c(188.114554495799, 160.424593244341,
167.169831096674, 166.203918308567, 170.724537947621, 175.021242542284,
161.591428698362, 166.072058276843, 175.075797678045, 190.707360569274,
142.569752983863, 174.685436684351, 170.63549056998, 184.975371979721,
161.664095244404)), class = "data.frame", row.names = c(NA, -15L
))

plot_ly(plot1, x = ~Power, y = ~gas, z = ~margin,
        marker = list(color = ~margin, 
                      colorscale = list(c(0, "rgb(227, 25, 54)"), list(1, "rgb(194, 205, 35)")),
                      showscale = F)) %>%
  add_markers() %>%
  layout(
    title = "Test",
    scene = list(xaxis = list(title = 'Electricity ($/MWh)'),
                 yaxis = list(title = 'Gas ($/GJ)'),
                 zaxis = list(title = 'Commodity Margin ($ million)'))
  )

Plotly test plot

关于R 中唯一的错误。default(x) : unique() applies only to vectors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56757997/

相关文章:

pandas - Period 类型的对象不是 JSON 可序列化的

r - 从 R 中数据框中的单元格中提取数字字符

python - 散点图的 Y 轴始终从 0 开始

r - 向可绘制的条形图添加水平线

r - 当一个元素包含一个函数时,按元素名称合并并封装多个相似的列表

python-3.x - 如何在 Plotly 3D Scatter 中更改颜色

r - 防止 Shiny 更新 plotly 的元素

r - glmnet:相同的 lambda 但 glmnet() 和 cv.glmnet() 的系数不同

仅适用于某些列的 r 子集数据框

r - 使文件可写以添加新包