Assignment 7

Author

Surafel Haile

Assignment 7 :

library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.5.3
Warning: package 'readr' was built under R version 4.5.3
Warning: package 'dplyr' was built under R version 4.5.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.2     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dslabs)
Warning: package 'dslabs' was built under R version 4.5.3
library(highcharter)
Warning: package 'highcharter' was built under R version 4.5.3
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
Highcharts (www.highcharts.com) is a Highsoft software product which is
not free for commercial and Governmental use

Attaching package: 'highcharter'

The following object is masked from 'package:dslabs':

    stars
library(RColorBrewer)

Here, the appropriate libraries are loaded .

help("gapminder")
starting httpd help server ... done

This function helps us understand the data set we’re considering.

data(gapminder)
head(gapminder)
              country year infant_mortality life_expectancy fertility
1             Albania 1960           115.40           62.87      6.19
2             Algeria 1960           148.20           47.50      7.65
3              Angola 1960           208.00           35.98      7.32
4 Antigua and Barbuda 1960               NA           62.97      4.43
5           Argentina 1960            59.87           65.39      3.11
6             Armenia 1960               NA           66.86      4.55
  population          gdp continent          region
1    1636054           NA    Europe Southern Europe
2   11124892  13828152297    Africa Northern Africa
3    5270844           NA    Africa   Middle Africa
4      54681           NA  Americas       Caribbean
5   20619075 108322326649  Americas   South America
6    1867396           NA      Asia    Western Asia

We proceed by loading “gap-minder”; the data set we’ll be exploring .

summary(gapminder)
                country           year      infant_mortality life_expectancy
 Albania            :   57   Min.   :1960   Min.   :  1.50   Min.   :13.20  
 Algeria            :   57   1st Qu.:1974   1st Qu.: 16.00   1st Qu.:57.50  
 Angola             :   57   Median :1988   Median : 41.50   Median :67.54  
 Antigua and Barbuda:   57   Mean   :1988   Mean   : 55.31   Mean   :64.81  
 Argentina          :   57   3rd Qu.:2002   3rd Qu.: 85.10   3rd Qu.:73.00  
 Armenia            :   57   Max.   :2016   Max.   :276.90   Max.   :83.90  
 (Other)            :10203                  NA's   :1453                    
   fertility       population             gdp               continent   
 Min.   :0.840   Min.   :3.124e+04   Min.   :4.040e+07   Africa  :2907  
 1st Qu.:2.200   1st Qu.:1.333e+06   1st Qu.:1.846e+09   Americas:2052  
 Median :3.750   Median :5.009e+06   Median :7.794e+09   Asia    :2679  
 Mean   :4.084   Mean   :2.701e+07   Mean   :1.480e+11   Europe  :2223  
 3rd Qu.:6.000   3rd Qu.:1.523e+07   3rd Qu.:5.540e+10   Oceania : 684  
 Max.   :9.220   Max.   :1.376e+09   Max.   :1.174e+13                  
 NA's   :187     NA's   :185         NA's   :2972                       
             region    
 Western Asia   :1026  
 Eastern Africa : 912  
 Western Africa : 912  
 Caribbean      : 741  
 South America  : 684  
 Southern Europe: 684  
 (Other)        :5586  

Summary () gives us some important general insights . For this assignment , we’ll pay attention to the null values. We’ll need to filter some of these values to make our final plot accurate.

gap_clean <- gapminder %>% 
  
  filter(!is.na(fertility),
         !is.na(infant_mortality),
         !is.na(gdp)
)

Gap_clean is our cleaned data set. With is code we’ve taken gap-minder and filtered out null values of the following columns : fertility, infant_mortality, and gdp.

cols<- brewer.pal(5,"Set2")


highchart() %>% 
  hc_add_series( gap_clean, "scatter", 
  hcaes( x = fertility, y = infant_mortality, group= continent) )%>%
  
  hc_colors(cols) %>% 
  
  hc_title(text = "Fertility vs Infant Mortality") %>% 
  
  hc_subtitle(text = " Grouped by Continents ") %>% 
  
  hc_xAxis(title = list(text = "Fertility Rate")) %>% 
  hc_yAxis(title = list(text = "Infant Mortality")) %>% 
  
  hc_caption(text = "Source: DS Labs") %>% 
  hc_add_theme(hc_theme_smpl())

This code creates an interactive scatter plot using the highcharter package. We first set five colors from the “Set2” palette using RColorBrewer then we plot the relationship between fertility rate (x-axis) and infant mortality (y-axis) from the gap_clean dataset, grouping points by continent so each group is shown in a different color.