Part(a)

Summarize and plot the median life expectancy (MLE) in 1952 for each of the 5 countries with the top MLE’s and for each of the 5 countries with the bottom MLE’s. You should have one summary table and one plot.

# Filtering and Assigning gapminder data to a variable 
 Data  <-gapminder %>%
  dplyr::filter(year==1952) %>%
  dplyr::group_by(country)
# Sorting Data from the Top              
top = arrange(Data, desc(lifeExp))
# Sorting Data from the Bottom
bottom = arrange(Data, lifeExp)

# Combing the Top and bottom rows to create a new dataframe
TopbottomDF <- rbind(top[1:5,],bottom[1:5,])
# Plotting Country against Life Expectancy of the new dataframe         
ggplot(TopbottomDF, aes(x = country, y = lifeExp))+
  geom_col(fill = "#006400")+
  labs(title = 'Top and Bottom five Countries in life Expectancy in 1952',
       x = 'Country', y = "life Expectancy")+
  theme(axis.text.x = element_text(angle = 45))

# Comments
"According to the graph and table above, there is a significant disparity 
in life expectancy between industrialized and developing nations, with the 
top and bottom countries having a gap of nearly 41 years."
## [1] "According to the graph and table above, there is a significant disparity \nin life expectancy between industrialized and developing nations, with the \ntop and bottom countries having a gap of nearly 41 years."

Part(b)

Summarize and plot the median life expectancy (MLE) in 2007 for each of the 5 countries with the top MLE’s and for each of the 5 countries with the bottom MLE’s. You should have one summary table and one plot.

# Filtering and Assigning gapminder data to a variable 
Data1  <-gapminder %>%
  dplyr::filter(year==2007) %>%
  dplyr::group_by(country)
# Sorting Data from the Top              "#FF9999"
top1 = arrange(Data1, desc(lifeExp))
# Sorting Data from the Bottom
bottom1 = arrange(Data1, lifeExp)

# Combing the Top and bottom rows to create a new dataframe
TopbottomDF1 <- rbind(top1[1:5,],bottom1[1:5,])

ggplot(TopbottomDF1, aes(x = country, y = lifeExp))+
  geom_col(fill =  "#FF9999")+
  labs(title = 'Top and Bottom five Countries in life Expectancy in 1952',
       x = 'Country', y = "life Expectancy")+
  theme(axis.text.x = element_text(angle = 45))

# Comments
"According to the graph and table above, the range of life expectancy in 2007 
is about 43 years; this has decreased and changed somewhat since 1952, 
but not much. The gap narrowed by almost a year after around a ten-year shift "
## [1] "According to the graph and table above, the range of life expectancy in 2007 \nis about 43 years; this has decreased and changed somewhat since 1952, \nbut not much. The gap narrowed by almost a year after around a ten-year shift "

Part(c)

Summarize and plot the median life expectancy in each year for the largest 5 countries in terms of 2007 population. You should have one summary table and one plot. Hint: Use the data in 2007 to find the top 5 countries in population. Then, plot MLE vs year for each of the 5 countries. You should have 5 curves and you should overlay them in one graph.

# Filtering the 5 largest countries in 2007
Data3  <-gapminder %>%
  dplyr::filter(year==2007) %>%
  dplyr::group_by(country) %>%
  dplyr::arrange(desc(pop))
Top5POp <- arrange(Data3[1:5,], desc(pop))

# Plotting the Median Life Expectancy of the 5 largest countries. 
Populated = select(filter(gapminder, country == 'China' | country == 'India' | 
                          country == 'United States' | country == 'Indonesia' |
                          country == 'Brazil'), c(country,year,lifeExp))
ggplot(Populated, aes(x = year, y = lifeExp, color = country))+
  geom_line()+
  labs(title = 'Top five countries in Population between 1952 and 2007', 
       x = 'Years', y = 'Life Expectancy', color = 'Country')

# Comment 
" The Life Expectancy of each Country has a positve trend as years went on"
## [1] " The Life Expectancy of each Country has a positve trend as years went on"

## Part(d) Summarize and plot the median life expectancy in each year for each continent. You should have one summary table and one plot.

# Restructuring of data into average mean life expectancy by continent
 Data4 = gapminder %>% group_by(continent, year) %>%
  mutate(lifeExp = sum(pop*lifeExp)/sum(pop)) %>%  
  subset(select = c(continent, year, lifeExp)) %>% unique()
 
 # Ploting the data 
 ggplot(Data4, aes(x = year, y = lifeExp, color = continent))+
   geom_line()+
   labs(title = 'Average Continent life Expectancy between 1952 and 2007', 
        x = 'Years', y = 'Life Expectancy', color = 'continent')

# Comments
 
 "The top three continents (the Americas, Europe, and Oceania) may be seen in 
 the graph and table above as having constantly rising average life expectancies. 
 Asia follows closely after them. We must, however, take heed of the fact that 
 Africa appears to be having difficulty extending life expectancy. that is, 
 it appears that between 1986 and 2003, the average life expectancy in Africa 
 remained same."
## [1] "The top three continents (the Americas, Europe, and Oceania) may be seen in \n the graph and table above as having constantly rising average life expectancies. \n Asia follows closely after them. We must, however, take heed of the fact that \n Africa appears to be having difficulty extending life expectancy. that is, \n it appears that between 1986 and 2003, the average life expectancy in Africa \n remained same."