2

I'd like to make a violin plot in plotly based on a violin plot in ggplot.

I looked at this similar question, but my use case is different since I'm not getting the same warning.

Right now, it's just showing a horizontal line without the boxplot or violin or outliers.

This is my code

library(fpp)
library(tidyverse)
library(plotly)

gg <-
  credit_old %>% 
  ggplot(aes(score, y = "")) +
  geom_violin(trim = T) +
  geom_boxplot(width = 0.1) +
  theme_ipsum_rc(grid = "XY") 

ggplotly(gg)
stefan
  • 90,330
  • 6
  • 25
  • 51
tadon11Aaa
  • 400
  • 2
  • 11

1 Answers1

2

To make a violin plot or boxplot via ggplotly you have to map on the y aesthetic. Plotting with flipped aesthetics is to the best of my knowledge at present not supported via ggplotly. Therefore, if you want a horizontal violin or boxplot you have to make use of coord_flip.

Using mtcars as example data try this:

library(plotly)

gg <-
  mtcars %>% 
  ggplot(aes(x = "", y = mpg)) +
  geom_violin(trim = T, fill = NA) +
  geom_boxplot(width = 0.1)

ggplotly(gg)

enter image description here

Horizontal box or violin plot:

gg + coord_flip()
ggplotly()

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51