R unique.default(x)中的Plotly错误:unique()仅适用于向量

时间:2019-06-25 16:00:27

标签: 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
))

1 个答案:

答案 0 :(得分:1)

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

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