Adding plots
1. Adding plots
Now let's discuss how to create visualizations of the data and include them in a report.2. Loading ggplot2
We can make plots the same way we would in the console. Simply put the code in a code chunk, and the plot will render when the file is knit. First, we'll need to load the ggplot2 package in the first code chunk in the document, as we did before with dplyr.3. Visualizing the annual summary
Let's use the Investment Annual Summary data to create a line plot to visualize the investment amount of dollars in millions across the different fiscal years. We call the ggplot function, and pass it the dataset name, followed by the aes function to specify the aesthetics of the plot, with x equal to the fiscal year, y equal to the dollars in millions invested, and color equals region. We then add the geom_line() function to create the line plot. Now we can see how the investment amount has varied over time across the different regions.4. Adding plot labels
Let's improve the plot by adding labels. After the geom_line() function, we add a plus sign and the labs() function to add labels for the title, and x and y axes. When we knit the file, we can see the plot with the added labels.5. Visualizing projects in Indonesia
Let's also create plots for the investment services projects data that we filtered previously. First we can create a plot for all projects occurring in Indonesia from the 2012 to 2018 fiscal years, and display each project and the total investment amount associated with each project as a scatterplot. We call the ggplot() function, indonesia_investment_projects, followed by the aes() function, with x equal to the date_disclosed, and y equal to the total_investment. We add the geom_point() function to create the scatterplot and include labels for the title and x and y axes. We knit the file, and see the resulting scatterplot that displays the projects and associated investment amounts.6. Visualizing project status
When we take another look at the indonesia_investment_projects data, we see that it includes a status column, which identifies the project's status as either Pending Approval, on Hold, Active, or Completed. Let's add color equals status, to color each point by project status. This plot provides a lot more clarity because we're able to see the date disclosed, associated investment amount, and status of each project. Notice that the knit file includes a warning message about the data that was excluded. Let's create another plot and continue to explore the data.7. Visualizing projects in Indonesia in 2012
We can add another plot using similar code and the data that was filtered for projects in Indonesia in the 2012 fiscal year. When we create this plot and knit the file, we see another warning message that one of the points was removed due to missing values. We'll discuss how to handle these warning messages in the report later in the course, but for now, let's explore why the data was removed.8. Missing values
If we take a closer look at the indonesia_investment_projects_2012 data, we can see each project from the 2012 fiscal year. Notice that the LMS Toll Project is on hold and has no investment values listed, including for the total investment. Since we used the investment amounts to create the plot, and the project is on hold and has no associated investment amounts, the data was excluded from the plot. This is another example of something we may want to include in the text of the report to provide more information to the audience.9. Let's practice!
Now that you can create and add plots to a report, let's practice!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.