gpt4 book ai didi

r - plotly 堆积的条形图 - 图例中的颜色不正确

转载 作者:行者123 更新时间:2023-12-01 06:05:08 25 4
gpt4 key购买 nike

我正在尝试在 plotly (plotly_4.5.6) 中绘制堆积条形图来自像这里的数据:

> dane
visit_source variable value
1 organic 2016 32
2 social 2016 20
3 hp 2016 24
4 branded 2016 24
5 organic 2015 25
6 social 2015 25
7 hp 2015 25
8 branded 2015 25

使用此代码:
library("dplyr")
library("plotly")

plot_ly(dane, y = ~variable, x = ~value, color = ~visit_source, text = ~paste0(value, "%"),
type = "bar", hoverinfo = "text+name", marker = list(line = list(color = "white", width = 2))) %>%
layout(barmode = "stack",
legend = list(orientation = "h"),
xaxis = list(ticksuffix = "%"),
yaxis = list(title = ""))

我得到:

enter image description here

但是当我修改我的 data.frame一点(只去掉一行),传说中的一些颜色变成黑色。像这儿:
plot_ly(dane[-1, ], y = ~variable, x = ~value, color = ~visit_source, text = ~paste0(value, "%"),
type = "bar", hoverinfo = "text+name", marker = list(line = list(color = "white", width = 2))) %>%
layout(barmode = "stack",
legend = list(orientation = "h"),
xaxis = list(ticksuffix = "%"),
yaxis = list(title = ""))

enter image description here

当我拥有所有 levelsvisit_source每个 yearvalue等于 0 ,那么它就起作用了,而且还会出现在 hover 中,我不喜欢。

你知道这里有什么问题吗?谢谢你的帮助!

我的数据:
structure(list(visit_source = structure(c(1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L), .Label = c("organic", "social", "hp", "branded"), class = "factor"),
variable = c("2016", "2016", "2016", "2016", "2015", "2015",
"2015", "2015"), value = c(32, 20, 24, 24, 25, 25, 25, 25
)), class = "data.frame", .Names = c("visit_source", "variable",
"value"), row.names = c(NA, -8L)) -> dane

最佳答案

尝试安装 Plotly 的最新开发者版本(4.5.6.9000) 它应该可以工作

enter image description here

df = data.frame(visit_source=c('organic', 'social', 'hp', 'branded', 'organic', 'social', 'hp', 'branded'), 
variable = c(rep('2016', 4), rep('2015',4)),
value=c(32, 20, 24, 24, 25, 25, 25, 25))


library("dplyr")
library("plotly")

plot_ly(df[-1,], y = ~variable, x = ~value, color = ~visit_source, text = ~paste0(value, "%"),
type = "bar", hoverinfo = "text+name", marker = list(line = list(color = "white", width = 2))) %>%
layout(barmode = "stack",
legend = list(orientation = "h"),
xaxis = list(ticksuffix = "%"),
yaxis = list(title = ""))

关于r - plotly 堆积的条形图 - 图例中的颜色不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41139470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com