简体   繁体   English

如何在 R Shiny App 的圆环图上使用 plotly

[英]how to use plotly on donut chart in R Shiny App

I have a donut chart that I want to make interactive in my R Shiny app using plotly.我有一个圆环图,我想使用 plotly 在我的 R Shiny 应用程序中进行交互。

When it is rendered as a plot, everything is fine:当它呈现为 plot 时,一切正常:

Using ggplot--looks right, but not interactive使用 ggplot——看起来不错,但不是交互式的

library(shiny)
library(dplyr)
library(ggplot2)
library(Cairo)
library(ggrepel)
library(plotly)
options(shiny.usecairo=T) #used to get the plot to look crisp in Shiny

my_colors <- c("#00A3AD", "#FF8200", "#753BBD", "#6CC24A")

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            
        ),
        mainPanel(
            fluidRow(
                plotOutput("count_by_person")
                
            ))
    ))
server <- function(input, output) {
    
    
    output$count_by_person <- renderPlot({
        
        data <- tibble(name = c("Justin", "Corey", "Sibley", "Kate"),
                       n = c(10, 30, 59, 1),
                       prop = c(10, 30, 59, 1)) %>%
            dplyr::arrange(desc(name)) %>%
            dplyr::mutate(text_y = cumsum(prop)-prop/2)
            
        
        ggplot(data, aes(x = 2, y = prop, fill = name)) +
            geom_bar(stat = "identity", color = "white") +
            coord_polar(theta = "y", start = 0)+
            geom_label_repel(aes(y = text_y, label = paste0(n, "\n", prop, "%")), force_pull = 100, nudge_x = 1) +
            scale_fill_manual(values = my_colors) +
            theme_void() +
            xlim(.5, 2.5)

    })
    
}
shinyApp(ui, server)

在此处输入图像描述

But, when I try to use plotly, the format gets messed up in an incredible way:但是,当我尝试使用 plotly 时,格式以一种令人难以置信的方式变得混乱:

Using ggplotly--interactive, but looks unexplainably wrong使用ggplotly--interactive,但看起来莫名其妙的错误

library(shiny)
library(dplyr)
library(ggplot2)
library(Cairo)
library(ggrepel)
library(plotly)
options(shiny.usecairo=T) #used to get the plot to look crisp in Shiny

my_colors <- c("#00A3AD", "#FF8200", "#753BBD", "#6CC24A")

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            
        ),
        mainPanel(
            fluidRow(
                plotlyOutput("count_by_person")
                
            ))
    ))
server <- function(input, output) {
    
    
    output$count_by_person <- renderPlotly({
        
        data <- tibble(name = c("Justin", "Corey", "Sibley", "Kate"),
                       n = c(10, 30, 59, 1),
                       prop = c(10, 30, 59, 1)) %>%
            dplyr::arrange(desc(name)) %>%
            dplyr::mutate(text_y = cumsum(prop)-prop/2)
            
        
        plot <- ggplot(data, aes(x = 2, y = prop, fill = name)) +
            geom_bar(stat = "identity", color = "white") +
            coord_polar(theta = "y", start = 0)+
            geom_label_repel(aes(y = text_y, label = paste0(n, "\n", prop, "%")), force_pull = 100, nudge_x = 1) +
            scale_fill_manual(values = my_colors) +
            theme_void() +
            xlim(.5, 2.5)
        
        ggplotly(plot)
    })
    
}
shinyApp(ui, server)

在此处输入图像描述

How do I get my donut plot to still look how it does in the first example but be interactive like the second one?我如何让我的甜甜圈 plot 看起来仍然像第一个示例中那样,但像第二个示例一样具有交互性?

You can use the ggiraph package with ggplot您可以将 ggiraph package 与 ggplot 一起使用

library(shiny)
library(dplyr)
library(ggplot2)
library(Cairo)
library(ggiraph)  #Need to intall this package
options(shiny.usecairo=T) #used to get the plot to look crisp in Shiny

my_colors <- c("#00A3AD", "#FF8200", "#753BBD", "#6CC24A")

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      
    ),
    mainPanel(
      fluidRow(
        ggiraphOutput("count_by_person") #need to update from plotOutput
        
      ))
  ))
server <- function(input, output) {
  
  
  output$count_by_person <- renderggiraph({ #need to update from renderPlot
    
    data <- tibble(name = c("Justin", "Corey", "Sibley", "Kate"),
                   n = c(10, 30, 59, 1),
                   prop = c(10, 30, 59, 1)) %>%
      dplyr::arrange(desc(name)) %>%
      dplyr::mutate(text_y = cumsum(prop)-prop/2)
    
    
   p <-  ggplot() + #assign as object in environment
      geom_bar_interactive(data = data, stat = "identity", color = "white", aes(x = 2, y = prop, fill = name, tooltip = paste0(n, "\n", prop, "%"))) + #Add tooltip to aes()
      coord_polar(theta = "y", start = 0)+
      scale_fill_manual(values = my_colors) +
      theme_void() +
      xlim(.5, 2.5)
   
   ggiraph(code = print(p)) #call object p
    
  })
  
}
shinyApp(ui, server)

例子

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

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