我想做的是:我正在使用Plotly,如果条形图不是黄色的(#FFF761)/如果动物不是兔子,我想将文本字体着色为白色。如果条形图是黄色的/动物是兔子,我想将文本字体着色为黑色。我可以在普通条形图中实现这一点,但它对堆叠条形图不起作用,所以我很困惑。
下面是一个例子:
library(plotly)
Color1 <- "#EC4239"
Color2 <- "#FC954C"
Color3 <- "#FFF761"
rbind(
c("red","#EC4239"),
c("orange","#FC954C"),
c("yellow","#FFF761")
) %>% as.data.frame() %>%
dplyr::rename(colors3_name = V1,
colors3_hex = V2) -> colors3
colors3$colors3_hex -> bar_colors
city <- rep(c("Berlin", "London"), each = 9)
year <- rep(rep(c(2010, 2011, 2012), each = 3), times = 2)
animal <- rep(c("cat", "dog", "rabbits"), times = 6)
amount <- c(14, 67, 73,
43, 50, 56,
21, 24, 28,
44, 32, 32,
73, 45, 56,
60, 67, 58
)
df <- as.data.frame(cbind(year, city, animal, amount))
schriftfarbe <- ifelse(bar_colors != "#FFF761", "#000000", "#ffffff")
fig <-
plot_ly(df,
y = ~amount,
x = factor(df$city, levels = unique(df$city)),
frame = ~year,
color = ~animal,
colors = ~bar_colors,
type = 'bar',
text=~df$amount,
textposition = 'auto',
textfont=list(color= schriftfarbe)
) %>%
layout(barmode = 'stack',
title = "<b>Test Plot (2010–2012)</b>",
titlefont = list(size = 20),
font = list(family = "calibri",size = 14),
separators = ',',
xaxis = list(title = list(text = "", standoff = 4),
zeroline = TRUE),
yaxis = list(title = "Amount",
range= c(0,200)),
legend = list(orientation = 'v'))
fig
先谢谢你了!
1条答案
按热度按时间oknwwptz1#
一个选项是通过内联CSS设置文本颜色,即将标签 Package 在
span
标签中,并根据所需颜色设置color
属性: