Creating a Scatter Plot and Fitting a Linear Regression Model

To draw a scatter plot of the electricity consumption per capita against the GDP per capita, and then fit a regression model. Let's begin by implementing the following steps:

  1. Use ggplot and specify the x and y aesthetics, as follows:
X=gdp_per_capita, Y=Electricity_consumption_per_capita 
  1. Make a scatter plot using geom_point. The points should be color differentiated by country (aes(color=Country) ).
  2. Set the appropriate x-axis limits (from 0-3,000).
  3. Add a linear regression model, stat_smooth (method=lm).

 

The points indicate a positive trend, which is also confirmed by the fitted line. In other words, electricity consumption is positively correlated to the GDP of a country: ...

Get Applied Data Visualization with R and ggplot2 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.