【问题标题】:R Plotly Error in unique.default(x): unique() applies only to vectors唯一.default(x)中的R Plotly错误:唯一()仅适用于向量
【发布时间】:2019-06-25 16:00:27
【问题描述】:

我是 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
))

【问题讨论】:

  • 欢迎来到 Stackoverflow!如果您能提供一个数据样本,那就太好了,这样我们就可以运行绘图代码并尝试为您提供帮助。例如,您可以将 dput() 的输出粘贴到您的 plot1 数据帧中 - 这将对我们有所帮助。
  • 更新了我的帖子
  • 能否也提供am 对象?
  • 您的代码似乎在您调用 plot_ly 函数时没有 color = ~am 参数运行。当我删除那个论点时,我得到了一个情节。
  • 非常感谢您的帮助。应该看到了,抱歉。

标签: r plotly


【解决方案1】:

从您对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)'))
  )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    相关资源
    最近更新 更多