1

I am trying to convert the following code to use a more sophisticated package such as ggplot to make the chart look nicer. Can anyone help me?

  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)

Some extra info. the code basically takes the input from a dropdown to index a dataframe and dynamically change the data plotted on the chart.

OwlieW
  • 217
  • 6
  • 13
  • 2
    Three tips so you would get help quicker: (1) your question seems unrelated to Shiny, strip all the Shiny code so only relevant code remains (make the example minimal) (2) show us what you tried already (3) show all the packages you are using at the start of the example – Edwin Jun 30 '17 at 11:02
  • Thank you, but I wanted to include a working example, I have tried to do it myself but couldnt get it to work. – OwlieW Jun 30 '17 at 11:04
  • 1
    [How to make a good reproducable example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – vladli Jun 30 '17 at 11:13

0 Answers0