I am developing a Shiny App for which I want to have the added features of a plotly graphic (like pop-ups) rather than the basic functionality of ggplot2. When I convert ggplot2 to a plotly object using ggplotly(), however, I get an error. I have deduced that the error is associated with the coord_flip() call in the ggplot2 object, which is necessary in order to make a horizontal box plot. How can I convert this ggplot2 object to a plotly object and successfully flip the axes to make the box plot horizontal?
This is my ggplot2 code:
tempdata <- data.frame(c(162531, 462027, 221338, 89235, 211478, 258402, 67649, 607215, 265274, 92385, 64087, 192379, 256153, 245354, 137672, 201511, 104981, 107228, 114276, 98699, 125971, 54592, 304146, 131465, 365801)) names(tempdata) <- c("temp") tempPalette <- colorNumeric(palette = rev(brewer.pal(5, "RdYlGn")), domain = tempdata) BoxPlot <- ggplot(tempdata, aes(x = 1, y = temp, color = temp)) + stat_boxplot(geom = "errorbar", width = 0.03, color = "blue", size = 1) + geom_boxplot(width = 0.03, color = "blue", size = 1, fill = "blue", alpha = 0.2) + geom_point(size = 5, alpha = 0.6) + expand_limits(y=0) + scale_y_continuous(breaks = c(0, max(ceiling(tempdata)))) + scale_color_gradientn(colors = tempPalette(range(tempdata))) + geom_vline(xintercept = 1, color = "black", size = 0.5) + coord_flip() + ylab("Incidence") + theme(aspect.ratio = 0.05)
and to convert to a plotly object:
BoxPlotly <- ggplotly(BoxPlot)
I received the following error when I run the last line of code: Error in if (tr$type %in% c(“bar”, “box”)) traces[[i]]$orientation <- “h” : argument is of length zero.
Help is appreciated!!
Note: I’m also willing to do this directly in plotly rather than converting a ggplot2 object, but I’m having ample troubles with that as well.
My first attempt at recreating the graphic I made using ggplot2:
plot_ly(tempdata, x = ~temp, type = "box") %>% #, boxpoints = "all") add_markers(x = ~temp, y = 0, opacity = 0.3, size = 1, color = ~temp, colors = tempPalette)
It makes no difference what number I set the size to, I can’t make the points any larger or smaller. My palette gets ignored; a plotly default gets used instead. The points don’t overlay on the box. So yeah, struggling.
My preference is to fix the conversion of the ggplot2 object rather than reinvent the wheel with plotly, but comments on either are welcome.