【问题标题】:I'm trying to add a legend to a chart in a Shiny R application我正在尝试向 Shiny R 应用程序中的图表添加图例
【发布时间】:2017-05-30 08:23:38
【问题描述】:

我正在尝试将图例文本添加到图表中,水平显示在与闪亮应用程序中的图表相同的框中。下面的代码创建了图表,但我似乎不知道如何添加要显示的图例。

  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)

  })

感谢您的帮助!

这是包含图表的框的代码。

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


  )
)

【问题讨论】:

  • 你试过添加legend() 吗?像这样:legend("topright", "(x,y)", pch = 1, title = "title")
  • 是的,但这只是为情节本身添加了一个图例,而不是在情节所在的框的底部
  • 好的,不知道这会是什么样子,你能分享一些代码吗?
  • 上面加了一些
  • 嗯,没有数据/完整脚本我无法重现该示例。是否要将图例添加到同一框中但在绘图区域之外的绘图右侧?

标签: r plot shiny


【解决方案1】:

这是一个小例子,其中 par 用于指定边距,而 inset 设置在绘图区域之外。更多详情请看这里: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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-21
    • 2017-08-22
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 2015-08-19
    相关资源
    最近更新 更多