0
votes

I've been attempting to create interactive bar plots in R using ggplot2 and plotly, but I can't seem to get interactive bars when using summarized data, e.g.: a count of the number rows when grouped according to some factor variable. For instance, using examples from the plolty site, when I input something like this:

dat <- data.frame(
    time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
    total_bill = c(14.89, 17.23)
)

ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
    geom_bar(stat="identity")

ggplotly()

I would like to get something like the graphs found here: https://plot.ly/ggplot2/geom_bar/. However, I instead get something perfectly identical, but no labels are displayed when I hover over the bars. This is true whether I'm running it in RStudio or in a Shiny session.

I've noticed that I do get some interactivity if I don't summarize the data, but this generates separate (though identical) labels for each step along the y-axis, which is kind of annoying, and it requires a bit of what feels like silliness in the call to ggplot (something like 'y=1'). In addition, non-summarized data generates interactivity for some bars, but only if the height of the bar is greater than 1.

Anyone have any idea what's going on and what I can do about it?

1
I also ran you code and I also don't have interactivity. bug?MLavoie

1 Answers

0
votes

Good morning, I only custom your colors with a vector c("red","blue"). Try this code:

library(ggplot2)
library(plotly)
dat <- data.frame(
  time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
  total_bill = c(14.89, 17.23)
)
ggplot(dat,aes(x=time,y=total_bill))+
  geom_bar(fill=c("red","blue"),stat="identity")
ggplotly()

enter image description here