0

The given plot generates a barplot using ggplot2 and plotly. I want to create a similar horizontal barplot using ggplotly(p). Tried using the attribute coord_flip() in geom_bar() but no help. Please help me and thanks.

library(plotly)
dat <- data.frame(
time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(14.89, 17.23))
p <- ggplot(data=dat, aes(x=time, y=total_bill)) +
geom_bar(stat="identity")
p <- ggplotly(p)
Ashmin Kaul
  • 860
  • 2
  • 12
  • 37
  • 3
    Possible duplicate of [Horizontal Barplot in ggplot2](https://stackoverflow.com/questions/10941225/horizontal-barplot-in-ggplot2) – J. Ring Oct 10 '17 at 06:40
  • Hey thanks for replying, but this is not a duplicate as it involves ggplot2 and plotly to make the plot interactive – Ashmin Kaul Oct 10 '17 at 07:00

1 Answers1

1

If you install the development version of ggplot2, you can change the orientation to horizontal in the plot object (as described here):

p <- ggplot(data=dat, aes(x=time, y=total_bill)) +
  geom_bar(stat="identity") +
  coord_flip()

l = plotly_build(p)
l$data[[1]]$orientation <- "h"
l

enter image description here

eipi10
  • 91,525
  • 24
  • 209
  • 285
  • Hey, thanks for replying, this is the error I get after running your code: Error in gg2list(p, width = width, height = height, tooltip = tooltip, : attempt to apply non-function – Ashmin Kaul Oct 10 '17 at 07:10
  • I can't reproduce that error with `ggplot2_2.2.1.9000` and `plotly_4.7.1`. However, [there have been compatibility issues between plotly and ggplot2 in the past](https://github.com/tidyverse/ggplot2/issues/1806). Which versions of ggplot2 and plotly are you using? – eipi10 Oct 10 '17 at 16:50
  • please help me with this post, https://stackoverflow.com/questions/47812506/customizing-the-sankey-chart-to-cater-large-datasets – Ashmin Kaul Dec 14 '17 at 11:59