R Shiny将基本情节转换为ggplot

时间:2017-06-30 10:46:56

标签: r plot ggplot2 shiny

我正在尝试将以下代码转换为使用更复杂的包(例如ggplot)来使图表看起来更好。任何人都可以帮助我吗?

  output$zscore_chart <- renderPlot({

    xvals <- 2:186 
    req(input$countrySelectZScore)
    idx_country = which(input$countrySelectZScore==esiCountries)



    max_ESI <- max(esiData_DF[idx_country, 2:186], na.rm=TRUE)
    max_GDP <- max(GDPData_DF[idx_country, 2:182], na.rm=TRUE)

    overall_max <- max(max_ESI, max_GDP)


    min_ESI <- min(esiData_DF[idx_country, 2:186], na.rm=TRUE)
    min_GDP <- min(GDPData_DF[idx_country, 2:182], na.rm=TRUE)

    overall_min <- min(min_ESI, min_GDP)


    foo = ts(esiData_DF[1, 2:186], frequency = 12, start = 2001)
    dates = seq(as.Date("2001-01-01"), by = "year", along = foo)

    plot(x = 2:186, y=esiData_DF[idx_country, 2:186], type = "l"
         , xlab = "", ylab = "", col="grey20", ylim = c(overall_min-0.5, overall_max), lwd=3,las=2, xaxt='n')
    mtext("Quarterly percentage changes", side=3, adj=0, line=0.1, cex=1, font=0.5)
    lines(xvals, GDPData_DF[idx_country, 2:186], col="green2", lwd=3)
    axis(1,at=seq(1,round(length(xvals)/12))*12,label=paste('  ',2002+seq(1,round(length(xvals)/12))))
    mtext("Economic Sentiment Indicators", side=3, adj=0, line=1.2, cex=2, font=2)

    legend(
      "bottom", 
      lty=c(1,1), 
      lwd=c(3,3), 
      col=c("grey20", "green2"), 
      legend = c("Economic Sentiment Indicator", "GDP")
      ,bty = "n"
      ,xjust = 0.5
      ,yjust = 0.5
      ,horiz = TRUE

    )
  }, height = 525)

一些额外的信息。代码基本上从下拉列表中获取输入以索引数据帧并动态更改在图表上绘制的数据。

0 个答案:

没有答案