试图在线寻找解决方案,但找不到
plot_ly(x = ecomm_yoy2$YOY, y = ecomm_yoy2$Brand, type = 'bar', orientation = 'h') %>%
layout(xaxis = list(title = "% YOY change in £ for June", dtick = 10)) %>%
layout(yaxis = list(categoryorder = "array", categoryarray = ecomm_yoy2$YOY)) %>%
add_annotations(text = ecomm_yoy2$YOY, showarrow = F, xshift = 25)
答案 0 :(得分:1)
以下是使用plotly的color
和colors
参数的可能解决方案:
ecomm_yoy2 <- data.frame(YOY = -19:20, Brand = 1:40)
plot_ly(ecomm_yoy2, x = ~YOY, y = ~Brand, type = 'bar', orientation = 'h', color = ~YOY < 0, colors = c("chartreuse3", "red"), name = ~ifelse(YOY < 0, "< 0", ">= 0")) %>%
layout(xaxis = list(title = "% YOY change in £ for June", dtick = 10)) %>%
layout(yaxis = list(categoryorder = "array", categoryarray = ~YOY)) %>%
add_annotations(text = ~YOY, showarrow = F, xshift = 25)
您可能想看看:
library(plotly)
library(listviewer)
schema(jsonedit = interactive())
通过它可以浏览plotly的可用迹线及其参数。
答案 1 :(得分:0)
一种简单的方法是:
计算一个新变量,其正值为'1',负值为'0',并将其附加到数据集。
在进行绘制时,根据刚创建的列的值,使用颜色美学对条形进行着色。