Trend graphs

The next part of the code specifies the graph of the trend in life expectancy using ggplot2. Let's look at each piece of code:

  # trend    output$trend = renderPlot({         thePlot = theData() %>%      group_by(continent, year) %>%      summarise(meanLife = mean(lifeExp)) %>%      ggplot(aes(x = year, y = meanLife, group = continent, colour = continent)) +      geom_line() + ggtitle("Graph to show life expectancy by continent over time")        if(input$linear){       thePlot = thePlot + geom_smooth(method = "lm")     }         print(thePlot)  })

The first line defines the output as a reactive plot. The second instruction uses chained dplyr instructions, as we saw in Chapter 1, Beginning R and Shiny, first to group the data by continent and year, and then to calculate the ...

Get Web Application Development with R Using Shiny - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.