Gecko17k
Gecko17k

Reputation: 43

Multiple plots from multiple data frames using ggvis in shiny in R

How do I plot multiple series from different data frames, with different numbers of rows on one ggvis plot?

    shinyServer(function(input, output, session) {
    vis1 <- reactive({ 
      df2 <-data.frame(csv1)
      df3 <- data.frame(csv2)
      long2 <- array()
      matrix2<-data.frame(Col1,Col2,Col3,Col4)
      matrix2$long2 = as.character(paste0("Col3: ",Col3,", Col4: ",Col4))
      matrix2 # What does this line do?
    })
vis1 %>% 
  ggvis(x = ~Col1, y = ~Col2,fill = ~Col4, key:= ~long2) %>%
  layer_points() %>%
  add_axis("x", title = "Col1") %>% 
  scale_numeric("x", domain = c(0, 130), nice = FALSE, clamp = TRUE)%>%
  add_axis("y", title = "Col2") %>%  
  scale_numeric("y", domain = c(0, 8000), nice = FALSE, clamp = TRUE)%>% 
  add_tooltip(function(matrix2){
  paste0("Col2: ", matrix2$Col2, "<br>", "Col1: ", matrix2$Col1, "<br>", " ", as.character(matrix2$long2))
  }, "hover") %>%
   layer_paths(data = df2, x = ~Col1,y = ~Col2,fill:="green") %>%

  bind_shiny("plot_col2")#,"plot_ui_col2")

  output$matrix2_table <- renderTable({
  vis1()[,c("Col1","Col2","Col3","Col4")]
})

This is most of the server.R code, with ui.R code that works fine with one series plotted on these axes. But trying to put the data from two data frames on these axes just returns

"Error in add_data(vis, data, data_name) : object 'df2' not found"

Doesn't even get to df3.

How do I get it to recognise df2, then use that data to plot the second series?

I get that plotting two series from the same data frame is done like this:

df %>% ggvis(~x,~y)  layer_points() %>%
df %>% layer_points(~x,~y1) 

There abouts, but how is it done with different numbers of points and from different data frames? Thank you.

Upvotes: 1

Views: 548

Answers (1)

Gecko17k
Gecko17k

Reputation: 43

I put everything in the same data frame, in the same columns, just added more rows, also added a colours column, so they would be plotted in different colours. The first thing to do was to make a much larger data frame in the unnamed function section at the top (only executes once).

Upvotes: 1

Related Questions