简体   繁体   中英

Creating a pie chart with a Shiny App

I have been working on designing an app that can subset data by reserve, category, use type, and years in order to create a bar graph of use over time and a pie chart which aggregates use between each of the three categories. Here is an example of what my data looks like:

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

I managed to fix my code from my last question (thank you so much for everyone's help!) so that now I can create the bar chart and a pie chart if the use statistic selected is users. I can't seem to figure out why I am unable to get a pie chart of user days to be displayed.

Here is what my code looks like:

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)

Anyway, I am very confused as to why only one pie chart works and the other does not and any help would be greatly appreciated.

Thank you!

Your call to pie() when input$UseStatistic == "UserDays" references a 3x2 data table, rather than the values you want to plot, as in your first call when input$UseStatistic == "Users" .

Edit the last full line of your code to:

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

and you should be good to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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