Nevedha Ayyanar
Nevedha Ayyanar

Reputation: 865

R - Customising the font colour of label in stacked bar chart using plot_ly()

I have plotted the stacked bar chart using plot_ly(). The colors of the stacks were so bright that the labels are not visible in the chart.

This is the data frame used

df <- data.frame("QuarterYear" = c("Q1 2019","Q1 2019","Q2 2019","Q2 2019","Q3 2019","Q3 2019","Q3 2019"), "Size" = c("Medium","Small","Large","Medium","Large","Medium","Small"),
                 "percentage" = c(98,2,29,71,13,74,13),
                 "n" = c(78,78,90,90, 100,100,100))

The color constants for the stacks are as follows:

tColor <<- setNames(
  c('#3b7d7c','#1c334e','#a8d18b'),#81b09b','#689782','#55afa9','#f7e961','#f4eda1'),
  c("Medium", "Small","Large")
)

I tried to change the font color using add_text(). But I am unable to achieve the required output

Size <- c('Small','Medium','Large')
df$Size <- factor(df$Size, levels = Size)

plot_ly(df, x = df$QuarterYear,
        y = df$percentage,
        type = 'bar',
        name = df$Size,
        text = paste(df$percentage,"%"),
        textposition = 'top',
        hoverinfo = 'text',
        hovertext = paste('Size: ', df$Size,
                          '<br> % of Total count: ', paste(df$percentage,"%")),
        color = df$Size,
        colors = tColor) %>%
  layout(yaxis = list(title = "% of Count", zeroline = FALSE, 
                      showline = FALSE, ticksuffix = "%"), barmode = 'stack',hoverlabel = list(bgcolor= 'white'),
         xaxis = list(zeroline = FALSE,showline = FALSE,
                      ticktext = paste(df$QuarterYear,'<br> (n = ', df$n,')', sep = ''),
                      tickmode = 'array')) %>%
  layout(legend = list(orientation = "h",
                       xanchor = "center",
                       x = 0.5,
                       y = -0.13))%>%
  add_annotations(text = paste0(df$percentage, "%"),
                  x = df$QuarterYear,
                  y = unlist(tapply(df$percentage, df$QuarterYear, FUN=cumsum))-(df$percentage/2),
                  showarrow = FALSE) %>%
  layout(xaxis = list(categoryorder = 'array',
                      categoryarray = df$QuarterYear)) %>%
  add_text(textfont = ifelse(df$Size %in% c("Medium","Small"),'white', 'black'))

Can anyone provide a suitable solution to change the font color according to the color of the stack?

Upvotes: 1

Views: 785

Answers (1)

stefan
stefan

Reputation: 123783

This can be achieved via add_text by passing the font colors to textfont via a list, i.e. textfont = list(color = ifelse(df$Size %in% c("Medium","Small"),'white', 'black'). Try this:

df <- data.frame("QuarterYear" = c("Q1 2019","Q1 2019","Q2 2019","Q2 2019","Q3 2019","Q3 2019","Q3 2019"), "Size" = c("Medium","Small","Large","Medium","Large","Medium","Small"),
                 "percentage" = c(98,2,29,71,13,74,13),
                 "n" = c(78,78,90,90, 100,100,100))

tColor <- setNames(
  c('#3b7d7c','#1c334e','#a8d18b'),#81b09b','#689782','#55afa9','#f7e961','#f4eda1'),
  c("Medium", "Small","Large")
)

Size <- c('Small','Medium','Large')
df$Size <- factor(df$Size, levels = Size)
# Vector of font colors based on order of Size
textcolors <- ifelse(df$Size[order(df$Size)] %in% c("Medium","Small"),'white', 'black')

library(plotly)

plot_ly(df, x = df$QuarterYear,
        y = df$percentage,
        type = 'bar',
        name = df$Size,
        text = paste(df$percentage,"%"),
        textposition = 'top',
        hoverinfo = 'text',
        hovertext = paste('Size: ', df$Size,
                          '<br> % of Total count: ', paste(df$percentage,"%")),
        color = df$Size,
        colors = tColor) %>% 
  add_text(textfont = list(color = textcolors),
           y = unlist(tapply(df$percentage, df$QuarterYear, FUN=cumsum))-(df$percentage/2), showlegend = FALSE) %>% 
  layout(yaxis = list(title = "% of Count", zeroline = FALSE, 
                      showline = FALSE, ticksuffix = "%"), barmode = 'stack',hoverlabel = list(bgcolor= 'white'),
         xaxis = list(zeroline = FALSE,showline = FALSE,
                      ticktext = paste(df$QuarterYear,'<br> (n = ', df$n,')', sep = ''),
                      tickmode = 'array')) %>%
  layout(legend = list(orientation = "h",
                       xanchor = "center",
                       x = 0.5,
                       y = -0.13))%>%
  layout(xaxis = list(categoryorder = 'array',
                      categoryarray = df$QuarterYear))

enter image description here

Upvotes: 1

Related Questions