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.