firmo23
firmo23

Reputation: 8404

Color specific added text in plotly bar chart

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

Upvotes: 0

Views: 2246

Answers (1)

tamtam
tamtam

Reputation: 3671

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

Plot

enter image description here

Upvotes: 1

Related Questions