本文介绍了在 plotly 条形图中添加颜色特定的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的图表中,我在每个条形之外添加了 "Value %"
,现在我只想更改此添加文本的颜色,而不是轴标签的文本或悬停文本.
In the chart below I have added "Value %"
outside of every bar and now I want to change only the color of this added text and not the axes labels' text or hovertext.
library(plotly)
Country<-c("EU","CHE","ITA")
Value<-c(3,2,1)
dat<-data.frame(Country,Value)
fig1 <- plot_ly(dat,
x = ~Value,
y = ~Country,
type = 'bar',
orientation = 'h',
hovertemplate = paste('%{y}', '<br>Uptake first dose (%): %{x}<br><extra></extra>'),
mode = 'text',
text = paste(Value, '%'),
textposition = 'outside',
arker = list(color = '#63bb47')
)
fig1 <- fig1 %>% layout(
font = list(color = '#a2a2a2'),font = list(color = '#a2a2a2'),
yaxis = list(fixedrange = TRUE,title="",
showgrid = FALSE, showline = FALSE, showticklabels = TRUE, domain= c(0, 0.85)),
xaxis = list(fixedrange = TRUE,title="",zeroline = FALSE, showline = FALSE, showticklabels = FALSE, showgrid = FALSE))
fig1
推荐答案
使用 textfont
参数,您可以调整条形标签的字体、大小和颜色.
With the textfont
argument you can adapt font, size and color of the bar labels.
例如这里我将颜色更改为红色:
For example here I changed the color to red:
library(plotly)
Country<-c("EU","CHE","ITA")
Value<-c(3,2,1)
dat<-data.frame(Country,Value)
fig1 <- plot_ly(dat,
x = ~Value,
y = ~Country,
type = 'bar',
orientation = 'h',
hovertemplate = paste('%{y}',
'<br>Uptake first dose (%): %{x}<br><extra></extra>'),
text = paste(Value, '%'),
textposition = 'outside',
#change color
textfont = list(color = "red"),
marker = list(color = '#63bb47')
)
fig1 <- fig1 %>% layout(
font = list(color = '#a2a2a2'),font = list(color = '#a2a2a2'),
yaxis = list(fixedrange = TRUE,title="",
showgrid = FALSE, showline = FALSE, showticklabels = TRUE, domain= c(0, 0.85)),
xaxis = list(fixedrange = TRUE,title="",zeroline = FALSE, showline = FALSE, showticklabels = FALSE, showgrid = FALSE))
fig1
情节
这篇关于在 plotly 条形图中添加颜色特定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!