【问题标题】:How to put ggplot in the renderPlot in shiny r?如何在闪亮的r中将ggplot放在renderPlot中?
【发布时间】:2020-08-28 08:08:17
【问题描述】:

我是闪亮的新手,将ggplot 放入renderPlot 时遇到问题。如果 ggplot 单独运行,它运行良好,但是当我开始将它放入 renderPlot 时,它什么也没有。

代码如下:

library(shiny)
library(shinyWidgets)
library(shinydashboard)
library(shinythemes)
library(leaflet)
library(magrittr)
library(rvest)
library(readxl)
library(dplyr)
library(maps)
library(ggplot2)
library(reshape2)
library(plotly)

cv_continent <- read.csv("coronavirus_continent.csv")

ui <- bootstrapPage(
  tags$head(includeHTML("gtag.html")),
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "VISUALISATION LE COVID-19", id="nav",

             tabPanel("GLOBAL",
                       sidebarPanel(
                           width= 20,
                           span(tags$i(h1("Visualiser la revolution globale du Covid-19 pendant 4 mois\n")), style="color:#045a8d"),
                           span(tags$i(h2("Diagramme en barre")), style="color:#045a8d"),
                           selectInput("condition","Choisir observation:",
                                       choices = c("Cas","NouveauxCas","Décès","NouveauxDécès"))
                        ),

                       plotOutput("image")

                      ),

             tabPanel("Continent",
                  sidebarLayout(
                      sidebarPanel(
                        span(tags$i(h4("Visualiser la revolution du Covid-19 par continent\n")), style="color:#045a8d"),
                        selectInput("continent","Choisir un continent:",
                                    choices = c("Asie","Europe","Amérique_du_Nord","Amérique_du_Sud","Océanie"))
                      ),
                      #mainPanel(
                        tabsetPanel(
                          tabPanel("Diagramme en barre pour chaque continent", plotlyOutput("barre")),
                          tabPanel("Diagramme sectoriel", plotlyOutput("sectoriel")),
                          tabPanel("Dendrogramme", plotlyOutput("dendrogramme"))
                        )
                    #  )

                    )     
             )
  )

)
server <- function(input, output){
 ContinentInput <- reactive({
    switch(input$continent,
           "Asie" = Asia,
           "Europe" = Europe,
           "Amérique_du_Nord" = North_America,
           "Amérique_du_Sud" = South_America,
           "Océanie" = Oceania)
  })


output$sectoriel <-renderPlot({

  sec <- cv_continent %>% 
      group_by(continent_level) %>%
      summarise(total=sum(cases)) %>%
      mutate(pourcentage= total/sum(total) * 100)  

      ggplot(sec, aes(x = "",fill= continent_level)) +
      geom_bar(aes(y=pourcentage),width=1,stat="identity")+
      coord_polar(theta = "y")+
      geom_text(size=1.5,aes(y=pourcentage,label = paste0(round(pourcentage,digits = 2), "%")), 
                position = position_stack(vjust = 0.5),color="black")+
      scale_fill_manual(values = c("Lightblue","#AD7366","Lightgreen","Orange","Coral","#254290"))+
      labs(x="",y="",title = "\n",fill= "Libellé de famille de métier")+
      theme_classic() + theme(axis.line = element_blank(),
                              axis.text = element_blank(),
                              axis.ticks = element_blank(),
                              plot.title = element_text(hjust = 4, color = "#666666"))
      })



}
shinyApp(ui=ui, server=server)

这里我想在Continent 部分和第二个tabPanel 名称Diagramme sectoriel 中绘图

如果您需要这里是 csv 和其他文件的链接:https://gitlab.com/Schrodinger168/practice/-/tree/master#

对此的任何帮助将不胜感激!谢谢!

【问题讨论】:

  • plotOutput("image") 我没有看到任何地方定义的“图像”。
  • 另外,output$sectoriel &lt;-renderPlot({ 不会返回任何内容。你需要一个return(sec)
  • plotOutput("image") 它对我有用,所以我没有放它。我想关注plotOutput("sectoriel")
  • 啊,好吧,我应该把return(sec)放在代码末尾的哪里?
  • 我输入了return(sec),但它不起作用

标签: r ggplot2 shiny


【解决方案1】:

试试这个

library(shiny)
library(shinyWidgets)
library(shinydashboard)
library(shinythemes)
library(leaflet)
library(magrittr)
library(rvest)
library(readxl)
library(dplyr)
library(maps)
library(ggplot2)
library(reshape2)
library(plotly)

cv_continent <- read.csv("coronavirus_continent.csv")

ui <- bootstrapPage(
  tags$head(includeHTML("gtag.html")),
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "VISUALISATION LE COVID-19", id="nav",

             tabPanel("GLOBAL",
                       sidebarPanel(
                           width= 20,
                           span(tags$i(h1("Visualiser la revolution globale du Covid-19 pendant 4 mois\n")), style="color:#045a8d"),
                           span(tags$i(h2("Diagramme en barre")), style="color:#045a8d"),
                           selectInput("condition","Choisir observation:",
                                       choices = c("Cas","NouveauxCas","Décès","NouveauxDécès"))
                        ),

                       plotOutput("image")

                      ),

             tabPanel("Continent",
                  sidebarLayout(
                      sidebarPanel(
                        span(tags$i(h4("Visualiser la revolution du Covid-19 par continent\n")), style="color:#045a8d"),
                        selectInput("continent","Choisir un continent:",
                                    choices = c("Asie","Europe","Amérique_du_Nord","Amérique_du_Sud","Océanie"))
                      ),
                      #mainPanel(
                        tabsetPanel(
                          tabPanel("Diagramme en barre pour chaque continent", plotOutput("barre")),
                          tabPanel("Diagramme sectoriel", plotOutput("sectoriel")),
                          tabPanel("Dendrogramme", plotOutput("dendrogramme"))
                        )
                    #  )

                    )     
             )
  )

)
server <- function(input, output){
 ContinentInput <- reactive({
    switch(input$continent,
           "Asie" = Asia,
           "Europe" = Europe,
           "Amérique_du_Nord" = North_America,
           "Amérique_du_Sud" = South_America,
           "Océanie" = Oceania)
  })


output$sectoriel <-renderPlot({

  cv_continent %>% 
      group_by(continent_level) %>%
      summarise(total=sum(cases)) %>%
      mutate(pourcentage= total/sum(total) * 100) %>% 
      ggplot( aes(x = "",fill= continent_level)) +
      geom_bar(aes(y=pourcentage),width=1,stat="identity")+
      coord_polar(theta = "y")+
      geom_text(size=1.5,aes(y=pourcentage,label = paste0(round(pourcentage,digits = 2), "%")), 
                position = position_stack(vjust = 0.5),color="black")+
      scale_fill_manual(values = c("Lightblue","#AD7366","Lightgreen","Orange","Coral","#254290"))+
      labs(x="",y="",title = "\n",fill= "Libellé de famille de métier")+
      theme_classic() + theme(axis.line = element_blank(),
                              axis.text = element_blank(),
                              axis.ticks = element_blank(),
                              plot.title = element_text(hjust = 4, color = "#666666"))
  })



}
shinyApp(ui=ui, server=server)

【讨论】:

  • 感谢您的想法,我现在对其进行了一些编辑,并且可以正常工作。我删除了ggplot 中的sec 并将%&gt;% 放在mutate 之后
  • 忽略了,抱歉!我编辑了答案以反映这一点
猜你喜欢
  • 2019-05-23
  • 2014-01-01
  • 1970-01-01
  • 2016-12-20
  • 1970-01-01
  • 2015-12-02
  • 1970-01-01
  • 1970-01-01
  • 2021-10-09
相关资源
最近更新 更多