【问题标题】:Why does my plotly donut plot not show up in R Shiny?为什么我的情节甜甜圈情节没有出现在 R Shiny 中?
【发布时间】:2021-09-01 16:11:19
【问题描述】:

我正在尝试在我的闪亮应用程序中构建一个甜甜圈图,该应用程序使用情节,类似于另一个问题的this one)。但是'我的情节从未出现。我研究了其他答案(例如 this onethis onethis one),但它们并没有让我更接近我的答案。

我做错了什么?

library(shiny)
library(dplyr)
library(plotly)

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

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"),
                       n = c(1, 3, 6),
                       prop = c(.1, .3, .6))

        plot_ly(data, aes(x = 2, y = prop, fill = name)) +
            geom_bar(stat = "identity", color = "white") +
            coord_polar(theta = "y", start = 0)+
            geom_text(aes(y = lab.ypos, label = paste0("n = ", n, ", \n", prop, "%")), color = "white")+
            scale_fill_manual(values = my_colors) +
            theme_void() +
            xlim(.5, 2.5)
    })
    
}
shinyApp(ui, server)

【问题讨论】:

  • 尝试使用ggplot(...) 而不是plot_ly,然后使用plotly::ggplotly 将您的绘图转换为绘图对象。不幸的是,我担心 ggplotly 无法将您的 ggplot donut 转换为 plotly donut。

标签: r plot shiny plotly r-plotly


【解决方案1】:

在将图像引入您的应用程序之前,请始终尝试在闪亮之外重现图像

data <- tibble(name = c("Justin", "Corey", "Sibley"),
                       n = c(1, 3, 6),
                       prop = c(.1, .3, .6))

plot_ly(data, aes(x = 2, y = prop, fill = name)) +
            geom_bar(stat = "identity", color = "white") +
            coord_polar(theta = "y", start = 0)+
            geom_text(aes(y = lab.ypos, label = paste0("n = ", n, ", \n", prop, "%")), color = "white")+
            scale_fill_manual(values = my_colors) +
            theme_void() +
            xlim(.5, 2.5)

如果您在脚本中运行代码,您会看到它返回 null,因为您正在尝试将 plotly 与 ggplot 语法一起使用

尝试从下面的代码开始生成一个基本的圆环图并阅读 plotly 文档here 进行修改:

library(shiny)
library(dplyr)
library(plotly)

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

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"),
                   n = c(1, 3, 6),
                   prop = c(.1, .3, .6))
    
    data %>% 
      plot_ly(values=~prop) %>%
      add_pie(hole = 0.6)

  })
  
}
shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-07
    • 2021-09-03
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 2016-08-24
    • 1970-01-01
    相关资源
    最近更新 更多