Home > Mobile >  How can I add the reactive y axis labels in shiny with ggplot
How can I add the reactive y axis labels in shiny with ggplot

Time:06-07


enter code here

I am working to create a reactive dashboard with shiny where the y axis would be according to input from user through "y = input$column1". I was able to plot the barchart but I would like to see the value on top of each bar. Sample data | quarter| P_activity | A_CP| B_CP 1 101 1600 2000 2 102 1230 2400 3 103 1500 2600

Server script

function(input, output){
  
  pData1 <- reactive({
    
    filteredData <- subset(allData, P_Activity == input$P)
    
    
    final <- data.table(filteredData)[,list(A_CP = sum(A_CP)),
                                      
                                      
                                      by = list(quarter)]
    
    return(final)
    
    
    
  })
  
  output$plot1 <- renderPlot({

    plot <- ggplot(data = pData1(), aes_string(x= "quarter", y = input$column1))
    plot <- plot   geom_col(fill = "blue")
    plot
     
  })

  }

ui script


dashboardPage(
  dashboardHeader(title = "SSSS"),
                  

 dashboardSidebar(
   sidebarMenu(
     menuItem(text ="Summary",tabName = "plots"),
     
     selectInput(inputId = "P", label = "Select a P:", choices = unique(allData$P_Activity), selected = "101"),
    
     selectInput(inputId = "column1", label = "Outcome", choices = c("A_CP"))
   )
 ),
 dashboardBody(
   
   tabItems(
     tabItem(tabName = "plots",
             plotOutput("plot1",width =350, height =200)  
             )
   )
 )
)

The Data frame has 3 attributes:Quarter,A_CP, B_CP, where the A_CP is plotting as input$column1 against quarter.

I have serched a lot but since the y is a reactive variable , it looks bit rare to find a solution. Any help would be much appreciated. Thanks a lot.

CodePudding user response:

I assume you are after something like this (I have cleaned up your code).

library(shiny)
library(shinydashboard)
library(data.table)

# Sample data
allData <- data.frame(
    quarter = 1:3,
    P_Activity = as.character(c(101, 102, 103)),
    A_CP = c(1600, 1230, 1500),
    B_CP = c(2000, 2400, 2600))

# UI
ui <- dashboardPage(
    
    dashboardHeader(title = "SSSS"),
    
    dashboardSidebar(
        sidebarMenu(
            menuItem(text ="Summary", tabName = "plots"),
            selectInput(
                inputId = "P", 
                label = "Select a P:", 
                choices = unique(allData$P_Activity), 
                selected = "101"),
            selectInput(
                inputId = "column1", 
                label = "Outcome", 
                choices = c("A_CP")))
    ),
    
    dashboardBody(
        tabItems(
            tabItem(
                tabName = "plots",
                plotOutput("plot1", width =350, height = 200)))
    )
)

# Server
server <- function(input, output){

    pData1 <- reactive({
        
        filteredData <- subset(allData, P_Activity == input$P)
        data.table(filteredData)[ , list(A_CP = sum(A_CP)), by = list(quarter)]
        
    })
    
    output$plot1 <- renderPlot({
        
        ggplot(
            data = pData1(), 
            aes_string(x = "quarter", y = input$column1, label = input$column1))  
            geom_col(fill = "blue")  
            geom_text(vjust = -0.5)  
            scale_y_continuous(expand = expansion(mult = c(0, 0.2)))

    })
    
}

# Run
shinyApp(ui = ui, server = server)

enter image description here

  • Related