使用ggplot和有光泽创建一个非常互动的情节

时间:2018-05-29 09:31:42

标签: r ggplot2 shiny plotly interactive

我一直在寻找许多资源,很难找到具有互动情节所有功能的可重复示例。我真的很想在闪亮的应用程序中使用这些功能,这些功能类似于plotly()中提供的选项:

  1. 刷一个区域并双击
  2. 时有一个缩放选项
  3. 可以选择删除与其图例相对应的点
  4. 在顶部悬停时显示点的x和y值
  5. 我无法在同一代码中找到所有这些功能。我个人希望每个散点图都应该具有这些功能,如果它们用于闪亮的应用程序中。由于ggplot()中的此功能,许多人更喜欢plotly()。在看到这个问题的答案后,我希望每个人都希望ggplot() alonf与shiny

    一起使用

    我已经创建了缩放功能,帮助我使用第二个和第三个功能,即在图例中单击组1时删除与组1对应的点的选项,并显示点> sx和y轴值鼠标悬停在它上面。感谢!!!

    以下是要开始使用的示例数据和代码。

    library(ggplot2)
    library(ggrepel)
    library(shiny)
    ui=fluidPage(uiOutput("plot"))
    server = function(input, output) {
      #creating a range variable for zooming 
      ranges <<- reactiveValues(x = NULL, y = NULL)
      #Creating sample Data
      Data=data.frame(Group=c("Group 2","Group 1","Group 3","Group 2","Group 5","Group 4","Group 6",
                              "Group 7","Group 4","Group 3","Group 1","Group 5","Group 6","Group 7",
                              "Group 2","Group 4","Group 6","Group 7","Group 3","Group 1"),
                      Fruit=c("apple","apple","apple","mango","apple","apple","apple","apple","mango","mango",
                              "mango","mango","mango","mango","orange","orange","orange","orange","orange",
                              "orange"),
                      Percentage=c(68.46846847,77.35849057,72.72727273,26.12612613,76.31578947,62.79069767,
                                   71.05263158,69.23076923,30.23255814,25,20.75471698,23.68421053,23.68421053,
                                   23.07692308,5.405405405,6.976744186,5.263157895,7.692307692,2.272727273,
                                   1.886792453))
      #Creating the plor function
      output$graph<-renderPlot({
        ggplot(Data,aes(y=reorder(Fruit,Percentage),x=Percentage,
                        color=Group,
                        label=paste0(round(Percentage),"%")))+
          geom_point(size=4)+
          scale_x_continuous(limits=c(0,100))+
          theme(panel.grid.major.y=element_line(color="gray90",size = 0.7),
                panel.background=element_blank(),
                strip.background=element_blank(),
                panel.border=element_rect(color="black",fill=NA,size=1))+
          labs(y="",x="% of people",color="")+
          # geom_text_repel(direction="y",nudge_y = 0.2, point.padding = 0.1, box.padding = 0.1)+
          coord_cartesian(xlim = ranges$x, ylim = ranges$y, expand = T)
      })
      #Rendering the plot with the brush parameter
      output$plot=renderUI(
        plotOutput("graph",dblclick = "plot1_dblclick",brush = brushOpts(id = "plot1_brush",resetOnNew = TRUE),height=450,width=900)  
      )
      #To observe and change the limits of the graph (zooming)
      observeEvent(input$plot1_dblclick, {
        brush <- input$plot1_brush
        if (!is.null(brush)) {
          ranges$x <- c(brush$xmin, brush$xmax)
          ranges$y <- c(brush$ymin, brush$ymax)
    
        } else {
          ranges$x <- NULL
          ranges$y <- NULL
        }
      })
    }
    shinyApp(ui=ui,server=server)
    

0 个答案:

没有答案