gpt4 book ai didi

r - plotly 否决 ggplot 2's scale_fill_manual' s 标签

转载 作者:行者123 更新时间:2023-12-04 14:46:09 27 4
gpt4 key购买 nike

我有一个包含周结束日期和流失值的示例数据集,可以是负值也可以是正值。在 ggplot2 中,我使用 scale_fill_manual()在作为组的值的符号上。
这可以很好地显示正值与负值的颜色。此外,标签会根据提供的标签进行重写。但是,如果我只是简单地将它做成一个 plotly 图,我就会丢失我的标签,而是将它们设置回 -1, 1 因子。是否 plotly 上不支持这一点,如果是这样,他们是另一种完成这项工作的方式

library(ggplot2)
library(plotly)

dt <- structure(list(date = structure(c(18651L, 18658L, 18665L, 18672L,
18679L, 18686L, 18693L, 18700L, 18707L, 18714L), class = c("IDate",
"Date")), churn = c(-3.27088948787062, -0.582518144525087, -0.125024925224327,
-0.333746898263027, -0.685714285714286, -0.340165549862042, 0.0601176470588235,
-0.119351608461635, -0.0132513279284316, -0.011201854099989)), row.names = c(NA,
-10L), class = c("data.table", "data.frame"))

plot_ggplot <- ggplot(dt, aes(x = date, y = churn * 100)) +
geom_bar(stat = "identity", aes(fill = factor(sign(churn)))) +
scale_fill_manual(
values = c("#4da63f", "#e84e62"),
breaks = c("-1", "1"),
labels = c("Growing base", "Declining base")
) +
ylim(-75, 25) +
labs(
title = "Weekly churn rate",
fill = "Legend"
)
plot_ggplot
ggplot output image
plot_ggplotly <- ggplotly(plot_ggplot)

plot_ggplotly
plotly output image

最佳答案

这行得通吗?

dt$base = ifelse(sign(dt$churn)>0, "Growing base","Declining base")
plot_ggplot <- ggplot(dt, aes(x = date, y = churn * 100)) +
geom_bar(stat = "identity", aes(fill = base)) +
scale_fill_manual(
values = c("#4da63f", "#e84e62"),
) +
ylim(-75, 25) +
labs(
title = "Weekly churn rate",
fill = "Legend"
)
plot_ggplot
plot_ggplotly <- ggplotly(plot_ggplot)
enter image description here
编辑:我刚刚阅读了评论,我认为这是建议的

关于r - plotly 否决 ggplot 2's scale_fill_manual' s 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70017937/

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