简体   繁体   English

R中使用ggvis从多个数据帧中绘制多个图

[英]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? 如何在一个ggvis图上绘制来自不同数据帧的多个序列,且行数不同?

    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. 这是大多数server.R代码,带有ui.R代码,可以很好地在这些轴上绘制一个系列。 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" “ add_data(vis,data,data_name)中的错误:找不到对象'df2'”

Doesn't even get to df3. 甚至没有进入df3。

How do I get it to recognise df2, then use that data to plot the second series? 如何获取它来识别df2,然后使用该数据绘制第二个系列?

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. 谢谢。

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). 首先要做的是在顶部的未命名函数部分中创建一个更大的数据框(仅执行一次)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM