0

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

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

2 Comments

Could you please explain this line? ggp$x$data[[6]]$textposition <- "right" Where could I find some docs about it? I am struggling to replicate this tableau chart in ggplotly public.tableau.com/app/profile/ben.butters7853/viz/… I think I am quite near but I do not know well how to format the tooltip like the tableau chart. Do you have any hint?
TBMK the only documentation is section 2.2 of the R plotly book. Basically you can use e.g. plotly_json(ggp) to have a look at the underlying data structure of the plotly object where the data element contains the specs of the traces which make up the chart, e.g. for your example the data object has seven elements: the first five are the single bars, the sixth is the text annotation (created from the geom_text) and then there is a seventh element which is used to draw the colorbar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.