0

I running into a problem with plotly right now. I'm translating ggplot2 graphs into interactive graphs. Here is an example code:

library(ggplot2)
library(plotly)

x <- sample(x = c(1,2), size = 100, replace = TRUE)
y <- sample(x = c(3,4), size = 100, replace = TRUE)


df <- data.frame(x, y)

df <- transform(df,
                x = ifelse(x == 1,"cat","dog"),
                y = ifelse(y == 3, "young","old")
                )

p <- ggplot(df, aes(x= x, fill = y)) +
  geom_bar(position = "stack") +
  coord_flip() +
  theme(legend.position="none")

ggplotly(p)

I would expect the ggplotly function to create an interactive version but it seems that it's not able to handle factor counts.

ggplot result: ggplot result

plotly translation: plotly translation with ggplotly

Any suggestions to solve the issue are welcome!

Paavo Pohndorff
  • 323
  • 1
  • 2
  • 17

0 Answers0