Stacked bar plots - not adding up right

Hello:

Every time I make a stacked bar plot from ggplot to plotly, the plot never looks the same. In my ggplot stacked bar plot, all bars add up to 1 but when I convert it to plotly, the bars are all over the place. Has anyone encountered this? I suspect it has something to do with how my data is formatted but I can’t figure it out. I can send people my code if that will help. Thanks!!!

1 Like

Hi, could you provide a simple example of this?

I’m experiencing this same issue. It appears to me that ggplot creates a display value of a cumulative value for the stacked bar plot. However, plotly alphabetizes the bars but maintains the earlier label. This makes the labels entirely useless. Here is a reproducible example:

library(reshape2)
library(ggplot2)
library(plotly)


df = data.frame("name" = c("A", "B", "C", "D", "A1"),
                "rep1" = c(37, 19, 93, 13, 121),
                "rep2" = c(26, 1, 54, 7, 123),
                "rep3" = c(21, 10, 34, 14, 211)
                )

melted_df = melt(df, variable.name = "sample", value.name = "counts")

p = ggplot(melted_df, aes(x = sample)) +
  geom_bar(aes(y = counts, fill = name), stat = "identity")
ggplotly(p)

Notice that the A1 values are now at the bottom of the plot but exhibit a value of 283 for example for rep1. However, the location of the bar is below the 200 tick mark on the y-axis.

Any help with this would be greatly appreciated.

1 Like