2

I am using the R programming language. I am trying to take different types of graphs (bar graphs, pie charts) and put them on the same page. I generated some fake data and made several graphs - then I put them together (see : Combining Different Types of Graphs Together (R))

  library(dplyr)
    library(ggplot2)
    library(cowplot)
    library(gridExtra)
    library(plotly)
    
    date= seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")
    
    var <- rnorm(731,10,10)
    
    
    group <- sample( LETTERS[1:4], 731, replace=TRUE, prob=c(0.25, 0.22, 0.25, 0.25) )
    
    data = data.frame(date, var, group)
    
    data$year = as.numeric(format(data$date,'%Y'))
    data$year = as.factor(data$year)
    
    ###Pie
    Pie_2014 <- data %>% 
        filter((data$year == "2014")) %>% 
        group_by(group) %>% 
        summarise(n = n())
    
    Pie_2014_graph = ggplot(Pie_2014, aes(x="", y=n, fill=group)) +
        geom_bar(stat="identity", width=1) +
        coord_polar("y", start=0) +ggtitle( "Pie Chart 2014") 
    
    
    Pie_2015 <- data %>% 
        filter((data$year == "2015")) %>% 
        group_by(group) %>% 
        summarise(n = n())
    
    Pie_2015_graph = ggplot(Pie_2015, aes(x="", y=n, fill=group)) +
        geom_bar(stat="identity", width=1) +
        coord_polar("y", start=0) +ggtitle( "Pie Chart 2015") 
    
    
    Pie_total = data %>% 
        group_by(group) %>% 
        summarise(n = n())
    
    Pie_total_graph = ggplot(Pie_total, aes(x="", y=n, fill=group)) +
        geom_bar(stat="identity", width=1) +
        coord_polar("y", start=0) +ggtitle( "Pie Chart Average") 
    
    ###bars
    
    Bar_years = data %>%
        group_by(year, group) %>%
        summarise(mean = mean(var))
    
    Bar_years_plot = ggplot(Bar_years, aes(fill=group, y=mean, x=year)) +
        geom_bar(position="dodge", stat="identity") + ggtitle("Bar Plot All Years")
    
    Bar_total = data %>%
        group_by(group) %>%
        summarise(mean = n())
    
    Bar_total_plot = ggplot(Bar_total, aes(x=group, y=mean, fill=group)) +
        geom_bar(stat="identity")+theme_minimal() + ggtitle("Bar Plot Average")
    
    
   #assembling the graphs can be done two different ways 
#first way
    g1 <- grid.arrange(Pie_2014_graph, Pie_2015_graph , Pie_total_graph, nrow = 1)
    g2 <- grid.arrange(Bar_total_plot, Bar_years_plot, nrow = 1)
    g = grid.arrange(g1, g2, ncol = 1)
    
    #second way
    
    # arrange subplots in rows
    top_row <- plot_grid(Pie_2014_graph, Pie_2015_graph, Pie_total_graph)
    middle_row <- plot_grid(Bar_years_plot, Bar_total_plot)
    
    # arrange our new rows into combined plot
    p <- plot_grid(top_row, middle_row, nrow = 2)
    p

enter image description here

From here, I am trying to use the plotly::ggplotly() command to make the above output "interactive" (move the mouse over the graphs and see labels). I know that this works for individual plots:

ggplotly(Bar_years_plot)

enter image description here

However, this command does not seem to work with the "cowplot" and the "gridExtra" outputs:

#gridExtra version:

 ggplotly(g)
Error in UseMethod("ggplotly", p) : 
  no applicable method for 'ggplotly' applied to an object of class "c('gtable', 'gTree', 'grob', 'gDesc')"

#cowplot version: (produces empty plot)

ggplotly(p)
Warning messages:
1: In geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
  geom_GeomDrawGrob() has yet to be implemented in plotly.
  If you'd like to see this geom implemented,
  Please open an issue with your example code at
  https://github.com/ropensci/plotly/issues
2: In geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
  geom_GeomDrawGrob() has yet to be implemented in plotly.
  If you'd like to see this geom implemented,
  Please open an issue with your example code at
  https://github.com/ropensci/plotly/issues

Does anyone know if there is a quick way to use the ggplotly() function for objects created with "gridExtra" or "cowplot"?

I know that with a bit of work, it might be possible using "htmltools":

library(htmltools)

doc <- htmltools::tagList(
    div(Pie_2014_graph, style = "float:left;width:50%;"),
    div(Pie_2015_graph,style = "float:left;width:50%;"),
    div(Pie_total_graph, style = "float:left;width:50%;"),
    div(Bar_years_plot, style = "float:left;width:50%;"),
    div(Bar_total_plot, style = "float:left;width:50%;"))
    
    save_html(html = doc, file = "out.html")

But I am not sure how to do this.

Can someone please show me how to make the collections of graphs interactive either using ggplotly() or with htmltools()?

Thanks.

1 Answer 1

2

You should apply ggplotly() to the individual graphs, not the collection graphs.

For example:

Pie_2014_graph = ggplotly(ggplot(Pie_2014, aes(x="", y=n, fill=group))    +
    geom_bar(stat="identity", width=1) +
    coord_polar("y", start=0) +ggtitle( "Pie Chart 2014") )
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your reply! This is what i have done already. I am trying to make the collection of graphs interactive. Do you know if this can be done? Thank you!
can you edit your post with the updated script that you're running? Using ggplotly() on the individual graphs should solve your issue
I know how to do them one by one. I am trying to see if its possible to them all at the same time. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.