使用Shiny App创建饼图

时间:2018-03-15 14:40:48

标签: r shiny

我一直致力于设计一个可以按照保留,类别,使用类型和年份对数据进行子集化的应用程序,以便创建一段时间内使用的条形图和一个饼图,该饼图汇总了三个类别中每个类别之间的使用情况。以下是我的数据的示例:

Reserve Category    Users   UserDays    Year
S       Research    31       9735        2002
S       Education   71       77          2002
S       Public      0         0          2002
S       Research    58       21596       2003
S       Education   387      509         2003
S       Public      188      219         2003
V       Research    43       642         2012
V       Education   12        60         2012
V       Public      874       2882       2012
V       Research    51        458        2013
V       Education   27        108        2013
V       Public     1003       1932       2013

我设法修复了我上一个问题的代码(非常感谢大家的帮助!)所以如果选择的使用统计信息是用户,现在我可以创建条形图和饼图。我似乎无法弄清楚为什么我无法获得用户日显示的饼图。

以下是我的代码:

library(shiny)
library(tidyverse)
library(ggplot2)
library(dplyr)


ReserveData <- read_csv("S_V_TOTALS.csv")


ui <- fluidPage(
  titlePanel("Data Analysis"), 
  sidebarLayout(
    sidebarPanel(
      selectInput("Reserve", "Reserve:", choices = c("S",    "V")),
  selectInput("UseStatistic", "Use Statistic:", choices = c("Users",     "UserDays")),
  checkboxGroupInput("Category", label = h3("Category"), 
                     choices = list("Research" , "Education" , "Public" ),selected = 'Research'),
  hr(),
  fluidRow(column(3, verbatimTextOutput("Category"))), 
  fluidRow(
    column(4,
           sliderInput("Years", label = h3("Years"), min = 2001, 
                       max = 2016, value = c(2001, 2016))
    )
  ),

  hr(),
  fluidRow(
    column(4, verbatimTextOutput("value")),
    column(4, verbatimTextOutput("Years")))

),
mainPanel(
  tableOutput("Categories"),
  plotOutput(outputId = "distPlot"),
  plotOutput("distPie")
    )
  )
)


server <- function(input, output) {

  ReserveDATA <- reactive({
    ReserveDATA <- ReserveData %>% filter(Reserve %in% input$Reserve) %>%
      filter(Category %in% input$Category) %>%
      filter(Year >= input$Years[1] & Year <= input$Years[2])
if(is.null(input$Category))
  return()
ReserveDATA
  })

  output$distPlot <- renderPlot({


    Plot <- ggplot(data = ReserveDATA(), aes_string(x='Year', y =   input$UseStatistic, fill = 'Category')) + geom_bar(stat = "identity", position=position_dodge())
    Plot2 <- Plot + theme_bw() + theme(panel.border = element_blank(), text =  element_text(family = "Verdana", face = "bold", size = 12), axis.title.x = element_text(margin = unit(c(5, 0, 0, 0), "mm")), axis.title.y = element_text(margin = unit(c(0, 5, 3, 0), "mm"), angle = 90), panel.grid.minor = element_blank(), panel.grid.major = element_blank(), axis.line = element_line(colour = "black")) + ggtitle(input$Reserve, "Reserve Use")
    Plot2

   })
  output$distPie <- renderPlot({
    Reserve_Data <- ReserveData %>% filter(Reserve %in% input$Reserve) %>% 
      filter(Year >= input$Years[1] & Year <= input$Years[2])
    if (input$UseStatistic == "Users") {
      Reserve_Data <- aggregate(.~Category, FUN = sum, data = Reserve_Data[ ,  2:3])
      pct <- round(Reserve_Data$Users/sum(Reserve_Data$Users)*100)
      lbls <- paste(Reserve_Data$Category, pct)
      lbls <- paste(lbls, "%", sep = "")
      pie(Reserve_Data$Users, labels = lbls, main = "Pie Chart of Total Reserve Use")
    }
    else if (input$UseStatistic == "UserDays"){
      Reserve_Data <- Reserve_Data[ ,-3]
      Reserve_Data <- aggregate(.~Category, FUN = sum, data = Reserve_Data[ , 2:3])
      pct <- round(Reserve_Data$UserDays/sum(Reserve_Data$UserDays)*100)
      lbls <- paste(Reserve_Data$Category, pct)
      lbls <- paste(lbls, "%", sep = "")
      pie(Reserve_Data, labels = Reserve_Data$Category,main = "Pie Chart of Total Reserve Use") 

    }


  })
}

shinyApp(ui, server)

无论如何,我很困惑为什么只有一个饼图有效而另一个没有,任何帮助都会非常感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

input$UseStatistic == "UserDays"引用3x2数据表而不是您要绘制的值时,调用pie(),就像在input$UseStatistic == "Users"时的第一次调用中一样。

将代码的最后一行编辑为:

pie(Reserve_Data$UserDays, labels = Reserve_Data$Category,main = "Pie Chart of Total Reserve Use")

你应该好好去。