rggplot2ggplotlygeom-col

cannot run ggplotly on a geom_col


This is the data https://github.com/Clairinechendra/sbucks1010/blob/main/p3.csv

Below is my code

p3_plot <- p3 %>% 
  ggplot(aes(x= size_ml, y= n, fill= category))+
  geom_col(position="stack") +
  theme_light()

ggplotly(p3_plot)

everytime i run the ggplotly it doesn't work even though i already import the library plotly. any solutions for this?


Solution

  • Unfortunately "it doesn't work" doesn't say much about what didn't work and how it didn't work (did you get an an error? A warning? A blank plot? No data? Or ...?)

    Below is a fully reproducible example. Please do a clean restart of R/RStudio (don't restore an older session), and then copy & paste the code below to verify that you can reproduce the plot.

    library(tidyverse)
    
    p3 <- read_csv("https://raw.githubusercontent.com/Clairinechendra/sbucks1010/main/p3.csv")
    p3_plot <- p3 %>% 
        ggplot(aes(x = size_ml, y = n, fill = category))+
        geom_col(position = "stack") +
        theme_light()
    
    library(plotly)
    ggplotly(p3_plot)
    

    enter image description here

    Two additional comments speculating as to the issue(s) you're having:

    1. Perhaps the issue was with trying to read the CSV directly through the link you give at the beginning of the post: if you read data from GitHub, make sure you read data as "raw" data (see the revised link I use inside read_csv).
    2. As an intermediate step, always inspect the ggplot object (here: p3_plot) prior to "plotly-fication". If that plot is empty/wrong, the issue is not with plotly.