R的plot_ly堆叠条形图只能显示具有不同颜色的多个数值。

huangapple go评论53阅读模式
英文:

The R plot_ly stacked bar chart can only show multiple values with different colors

问题

I am programming a R-application, where I need to visualize data in stacked bar-charts. So far everything works fine if there are multiple bars shown, but I am getting empty graphs when I want to show only one bar.

So I created different plots with some dummy data, that shows my problem. (The data is fictitious)

data <- data.table(FRUIT = c("apples", "bananas", "kiwis", "apples", "bananas", "kiwis"), 
                    KCALS = c(100, 200, 130, 100, 200, 130), 
                    DATE = as.Date(c("2022-10-01", "2022-10-01", "2022-10-01", "2022-10-02", "2022-10-03", "2022-10-03")))

R的plot_ly堆叠条形图只能显示具有不同颜色的多个数值。

The first graph shows an overview over multiple dates:

plot_ly(data, x = ~DATE, y = ~KCALS, color = ~FRUIT, type = "bar") %>%
    layout(yaxis = list(title = " "), xaxis = list(title = " "), barmode = "stack")

R的plot_ly堆叠条形图只能显示具有不同颜色的多个数值。

The second graph shows only one day:

plot_ly(data[DATE == "2022-10-01"], x = ~DATE, y = ~KCALS, type = "bar") %>%
    layout(yaxis = list(title = " "), xaxis = list(title = " "), barmode = "stack")

R的plot_ly堆叠条形图只能显示具有不同颜色的多个数值。

Ok, that worked. But if I enable the "color-option" again, the plot is empty:

plot_ly(data[DATE == "2022-10-01"], x = ~DATE, y = ~KCALS, color = ~FRUIT, type = "bar") %>%
    layout(yaxis = list(title = " "), xaxis = list(title = " "), barmode = "stack")

R的plot_ly堆叠条形图只能显示具有不同颜色的多个数值。

Why is it behaving like this? What am I doing wrong? And is there any documentation available, that really describes how the data gets processed in the plot_ly()- function?

英文:

I am programming a R-application, where I need to visualize data in stacked bar-charts. So far everything works fine if there are multiple bars shown, but I am getting empty graphs when I want to show only one bar.

So I created different plots with some dummy data, that shows my problem. (The data is fictitious)

data <- data.table(FRUIT = c("apples", "bananas", "kiwis", "apples", "bananas", "kiwis"), 
                    KCALS = c(100, 200, 130, 100, 200, 130), 
                    DATE = as.Date(c("2022-10-01", "2022-10-01", "2022-10-01", "2022-10-02", "2022-10-03", "2022-10-03")))

R的plot_ly堆叠条形图只能显示具有不同颜色的多个数值。

The first graph shows an overview over multiple dates:

plot_ly(data, x = ~DATE, y = ~KCALS, color = ~FRUIT, type = "bar") %>%
    layout(yaxis = list(title = " "), xaxis = list(title = " "), barmode = "stack")

R的plot_ly堆叠条形图只能显示具有不同颜色的多个数值。

The second graph shows only one day:

plot_ly(data[DATE == "2022-10-01"], x = ~DATE, y = ~KCALS, type = "bar") %>%
    layout(yaxis = list(title = " "), xaxis = list(title = " "), barmode = "stack")

R的plot_ly堆叠条形图只能显示具有不同颜色的多个数值。

Ok, that worked. But if I enable the "color-option" again, the plot is empty:

plot_ly(data[DATE == "2022-10-01"], x = ~DATE, y = ~KCALS, color = ~FRUIT, type = "bar") %>%
    layout(yaxis = list(title = " "), xaxis = list(title = " "), barmode = "stack")

R的plot_ly堆叠条形图只能显示具有不同颜色的多个数值。

Why is it behaving like this? What am I doing wrong? And is there any documentation available, that really describes how the data gets processed in the plot_ly()- function?

答案1

得分: 2

当我运行您的代码并使用color变量时,它确实返回一个单一数值的堆叠条形图。请确保您没有覆盖变量并尝试在干净的环境中运行它。以下是一些可重现的代码:

library(data.table)
data <- data.table(FRUIT = c("apples", "bananas", "kiwis", "apples", "bananas", "kiwis"), 
                   KCALS = c(100, 200, 130, 100, 200, 130), 
                   DATE = as.Date(c("2022-10-01", "2022-10-01", "2022-10-01", "2022-10-02", "2022-10-03", "2022-10-03")))
library(plotly)
plot_ly(data, x = ~DATE, y = ~KCALS, color = ~FRUIT, type = "bar") %>%
  layout(yaxis = list(title = " "), xaxis = list(title = " "), barmode = "stack")

R的plot_ly堆叠条形图只能显示具有不同颜色的多个数值。

plot_ly(data[DATE == "2022-10-01"], x = ~DATE, y = ~KCALS, type = "bar") %>%
  layout(yaxis = list(title = " "), xaxis = list(title = " "), barmode = "stack")

R的plot_ly堆叠条形图只能显示具有不同颜色的多个数值。

plot_ly(data[DATE == "2022-10-01"], x = ~DATE, y = ~KCALS, color = ~FRUIT, type = "bar") %>%
  layout(yaxis = list(title = " "), xaxis = list(title = " "), barmode = "stack")

R的plot_ly堆叠条形图只能显示具有不同颜色的多个数值。

Created on 2023-02-16 with reprex v2.0.2

在这里,您可以找到关于plotly中条形图的文档

英文:

When I run your code with color variable it does return a stacked bar plot for a single value. Make sure you don't overwrite variables and try it with a clean environment. Here is some reproducible code:

library(data.table)
data &lt;- data.table(FRUIT = c(&quot;apples&quot;, &quot;bananas&quot;, &quot;kiwis&quot;, &quot;apples&quot;, &quot;bananas&quot;, &quot;kiwis&quot;), 
                   KCALS = c(100, 200, 130, 100, 200, 130), 
                   DATE = as.Date(c(&quot;2022-10-01&quot;, &quot;2022-10-01&quot;, &quot;2022-10-01&quot;, &quot;2022-10-02&quot;, &quot;2022-10-03&quot;, &quot;2022-10-03&quot;)))
library(plotly)
plot_ly(data, x = ~DATE, y = ~KCALS, color = ~FRUIT, type = &quot;bar&quot;) %&gt;%
  layout(yaxis = list(title = &quot; &quot;), xaxis = list(title = &quot; &quot;), barmode = &quot;stack&quot;)

R的plot_ly堆叠条形图只能显示具有不同颜色的多个数值。<!-- -->

plot_ly(data[DATE == &quot;2022-10-01&quot;], x = ~DATE, y = ~KCALS, type = &quot;bar&quot;) %&gt;%
  layout(yaxis = list(title = &quot; &quot;), xaxis = list(title = &quot; &quot;), barmode = &quot;stack&quot;)

R的plot_ly堆叠条形图只能显示具有不同颜色的多个数值。<!-- -->

plot_ly(data[DATE == &quot;2022-10-01&quot;], x = ~DATE, y = ~KCALS, color = ~FRUIT, type = &quot;bar&quot;) %&gt;%
  layout(yaxis = list(title = &quot; &quot;), xaxis = list(title = &quot; &quot;), barmode = &quot;stack&quot;)

R的plot_ly堆叠条形图只能显示具有不同颜色的多个数值。<!-- -->

<sup>Created on 2023-02-16 with reprex v2.0.2</sup>


Here you can find the docs about bar charts in plotly.

huangapple
  • 本文由 发表于 2023年2月16日 17:00:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定