rggplot2plotlyggplotlystacked-bar-chart

How to adjust text labels and color gradient in ggplotly stacked bar-chart?


When I convert my ggplot stacked bar chart to ggplotly, there are two issues that I can't solve:

  1. the text labels at the end of the bars change position unexpectedly. I would like them to keep their position, i.e. outside the bars.

  2. I can't add the gradient color to all the bars so that the first bar is darker and the last ones are lighter. I tried the code below bu it is not working.

scale_fill_gradient(high = "#24336a", low = "#2296cf")

Could you please help me?

library(tidyverse)
library(ggplot2)
library(scales)
library(plotly)

data_test <- tribble(
  ~name, ~cost1, ~cost2, ~totalCost,
  "John", 10, 40, 50,
  "Martin", 21, 35, 56,
  "Michael", 15, 80, 95,
  "Dave", 28, 40, 68,
  "Dan", 18, 35, 53
)
View(data_test)

df <- data_test %>%
  pivot_longer(cols = c("cost1", "cost2"),
               names_to = "cost",
               values_to = "value")

chart <- ggplot(df, aes(x=reorder(name, value), y=value)) +
  geom_col(fill = "#24336a") +
  geom_text(
    aes(label = after_stat(y), group = name),
    stat = 'summary', fun = sum, vjust = 0.5, hjust = -0.5) +
  coord_flip()

chart

enter image description here

ggplotly(chart)

enter image description here


Solution

  • TBMK h/vjust will have no effect on the text positions when converting a ggplot to plotly. Instead you can manipulate the plotly object to set the textposition for the labels. Additionally I use nudge_x to add some padding.

    Concerning your second issue, if you want to apply a scale then you have to map on aesthetics. To this end I use stat="summary" for the bars as well and map the value on the fill aes:

    library(plotly)
    
    chart <- ggplot(df, aes(y = reorder(name, value), x = value)) +
      geom_bar(aes(fill = after_stat(x)), stat = "summary", fun = sum) +
      geom_text(
        aes(
          label = after_stat(x)
        ),
        stat = "summary", fun = sum,
        nudge_x = 1
      ) +
      scale_fill_gradient(high = "#24336a", low = "#2296cf")
    
    ggp <- ggplotly(chart)
    
    ggp$x$data[[6]]$textposition <- "right"
    
    ggp
    

    enter image description here