簡體   English   中英

如何在 R Shiny App 的圓環圖上使用 plotly

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

我有一個圓環圖,我想使用 plotly 在我的 R Shiny 應用程序中進行交互。

當它呈現為 plot 時,一切正常:

使用 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)

在此處輸入圖像描述

但是,當我嘗試使用 plotly 時,格式以一種令人難以置信的方式變得混亂:

使用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)

在此處輸入圖像描述

我如何讓我的甜甜圈 plot 看起來仍然像第一個示例中那樣,但像第二個示例一樣具有交互性?

您可以將 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