使用“重新排序”时,根据数字向量着色不起作用在 x 轴上

发布于 2025-01-20 17:24:23 字数 1090 浏览 3 评论 0原文

library(dplyr)
library(plotly)

# data frame
Title = c('Titanic', 'Avatar', 'Jurassic World')
Profit = c(458672302, 523505847, 502177271)
df = data.frame(Title, Profit)

基本上,我正在尝试根据PROUT列来color。而且,如果未订购,它可以工作图1

# X axis not ordered (working)
plot_ly(df, x = ~Title,
            y = ~Profit,
            color = ~Profit,
            type = 'bar')

但是当我尝试重新订购时/code> <代码> x axis(看起来像 this ),它返回错误,可能是因为它是 factor

# X axis reordered (not working)
plot_ly(df, x = ~reorder(Title, -Profit) %>% as.character(),
            y = ~Profit,
            color = ~Profit,
            type = 'bar')

summary.factor中的错误(c(3L,1L,2L),na.rm = true): “范围”对因素没有意义 另外:警告消息: textfont.color(尚未)支持数据数组

是否有任何提示?

library(dplyr)
library(plotly)

# data frame
Title = c('Titanic', 'Avatar', 'Jurassic World')
Profit = c(458672302, 523505847, 502177271)
df = data.frame(Title, Profit)

Basically, I'm trying to color the bars according to the Profit column. And when it's not ordered, it works figure 1

# X axis not ordered (working)
plot_ly(df, x = ~Title,
            y = ~Profit,
            color = ~Profit,
            type = 'bar')

But when I try to reorder the x axis (to look like this), it returns an error, probably because it's a factor:

# X axis reordered (not working)
plot_ly(df, x = ~reorder(Title, -Profit) %>% as.character(),
            y = ~Profit,
            color = ~Profit,
            type = 'bar')

Error in Summary.factor(c(3L, 1L, 2L), na.rm = TRUE) :
‘range’ not meaningful for factors
In addition: Warning message:
textfont.color doesn't (yet) support data arrays

Does anyone have any tips?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坠似风落 2025-01-27 17:24:23

您的代码对我来说效果很好:

library(dplyr)
library(plotly)
  
# data frame
Title = c('Titanic', 'Avatar', 'Jurassic World')
Profit = c(458672302, 523505847, 502177271)
df = data.frame(Title, Profit)
  
# X axis reordered (not working)
plot_ly(df, x = ~reorder(Title, -Profit) %>% as.character(),
        y = ~Profit,
        color = ~Profit,
        type = 'bar')

输出:

在此处输入图像描述

这是我的 sessioninfo

R version 4.1.0 (2021-05-18)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS 12.3.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] nl_NL.UTF-8/nl_NL.UTF-8/nl_NL.UTF-8/C/nl_NL.UTF-8/nl_NL.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] plotly_4.10.0 ggplot2_3.3.5 dplyr_1.0.8 

因此您可以使用 sessioninfo() 检查您的会话你的控制台并检查是否你的包裹是对的。

Your code works fine for me:

library(dplyr)
library(plotly)
  
# data frame
Title = c('Titanic', 'Avatar', 'Jurassic World')
Profit = c(458672302, 523505847, 502177271)
df = data.frame(Title, Profit)
  
# X axis reordered (not working)
plot_ly(df, x = ~reorder(Title, -Profit) %>% as.character(),
        y = ~Profit,
        color = ~Profit,
        type = 'bar')

Output:

enter image description here

This is my sessioninfo:

R version 4.1.0 (2021-05-18)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS 12.3.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] nl_NL.UTF-8/nl_NL.UTF-8/nl_NL.UTF-8/C/nl_NL.UTF-8/nl_NL.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] plotly_4.10.0 ggplot2_3.3.5 dplyr_1.0.8 

So what you can do is check your session using sessioninfo() in your console and check if you packages are right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文