简体   繁体   中英

I'm trying to add a legend to a chart in a Shiny R application

I am trying to add legend text to a chart, horizontally to appear in the same box as the chart in a shiny application. The code below creates the chart but I cant seem to work out how to add a legend to show up.

  output$probability_chart <- renderPlot({
    req(input$countrySelectProbabilities)
    idx <- which(input$countrySelectProbabilities==FVI_DATA_ALL_COUNTRIES)
    plot(x = dates, y=Composite_Probabilities_1_DF[idx, 2:18], type = "l"
         , xlab = "Dates", ylab = "Probability", col="black", ylim = c(0, 100), lwd=2)
    lines(dates, Banking_Probabilities_1_DF[idx, 2:18], col="red", lwd=2)
    lines(dates, Currency_Probabilities_1_DF[idx, 2:18], col="green", lwd=2)
    lines(dates, Sovereign_Probabilities_1_DF[idx, 2:18], col="yellow", lwd=2)
    lines(dates, Sudden_Stop_Probabilities_1_DF[idx, 2:18], col="blue", lwd=2)

  })

Thank you for any help!

This is the code for the box where the chart is contained.

frow3 <- fluidRow(

  box(
    title = "Z - Scores by Country and Crisis Type"
    ,selectInput('countrySelectZScore', 'Country', mylist, width = "180px")
    ,radioButtons("z_score_crisis_type","Crisis Type", c("Global Normalisation" = "Global Normalisation", 
                                                       "Regional Normalisation" = "Regional Normalisation", 
                                                       "Own Country Normalisation" = "Own Country Normalisation"), inline=T)

    ,status = "primary"
    ,solidHeader = TRUE 
    ,collapsible = TRUE 
    ,plotOutput("zscore_chart")
    , height = 600
    , width = 6
  )



  ,box(
    title = "Probabilities by Country and Crisis Type"
    ,selectInput('countrySelectProbabilities', 'Country', mylist, width = "180px")
    ,radioButtons("probability_crisis_type","Crisis Type", c("Global Normalisation" = "Global Normalisation", 
                                                       "Regional Normalisation" = "Regional Normalisation", 
                                                       "Own Country Normalisation" = "Own Country Normalisation"), inline=T)
    ,status = "primary"
    ,solidHeader = TRUE 
    ,collapsible = TRUE 
    ,plotOutput("probability_chart")
    , height = 600
    , width = 6


  )
)

This is a small example where par is used to specify the margins and inset is set outside the plotting region. See here for more details: Plot a legend outside of the plotting area in base graphics?

library(shiny)

ui <- fluidPage(
  title = "Hello Shiny!",
  fluidRow(
    box(plotOutput("TEST"))
  )
)


server <- function(input, output) {
  output$TEST <- renderPlot({
    par(mar=c(5.1, 4.1, 4.1, 8.1), xpd=TRUE)
    plot(1:20,20:1,type="l")
    legend("topright", inset=c(-0.2,0.4), legend=c("A","B"), pch=c(1,3), title="Group")

  })
  }


shinyApp(ui = ui, server = server)

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