# install.packages("gapminder")
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.7
## v tidyr 1.1.4 v stringr 1.4.0
## v readr 2.1.1 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(gapminder)
library(ggplot2)
data(gapminder)
# scatter plot of Germany
gapminder %>%
filter(country=="Germany") %>%
ggplot(aes(x = year, y = pop)) +
ggtitle(label = "Scatter plot of Germany population vs Time") +
geom_point() +
theme(plot.title = element_text(size = 20, face = "bold"))
# line plot of germany population
gapminder %>%
filter(country=="Germany") %>%
ggplot(aes(x = year, y = pop)) +
ggtitle(label = "Time plot for Germany population") +
geom_line() +
theme(plot.title = element_text(size = 20, face = "bold"))
The line graph is better than the scatter plot. The line graph shows the trend of population growth over time while a scatter plot shows the linear relationship between time and population.
# scatter plot for Switzerland
gapminder %>%
filter(country=="Switzerland") %>%
ggplot(aes(x = year, y = pop)) +
ggtitle(label = "Scatterplot for Switzerland population vs time") +
geom_point() +
theme(plot.title = element_text(size = 20, face = "bold"))
# line plot of Switzerland population
gapminder %>%
filter(country=="Switzerland") %>%
ggplot(aes(x = year, y = pop)) +
ggtitle(label = "Time plot for Switzerland population") +
geom_line() +
theme(plot.title = element_text(size = 20, face = "bold"))
## Histogram of life expectancy in 2007
gapminder %>%
subset(gapminder$year==2007) %>%
select(country, continent, lifeExp) %>%
ggplot(aes(x=lifeExp, fill = continent)) +
geom_histogram(bins = 15) +
ggtitle(label = "Histogram of life expectancy in 2007") +
theme(plot.title = element_text(size = 20, face = "bold"))
gapminder %>%
select(continent, year, lifeExp) %>%
ggplot(aes(x=lifeExp, fill=continent)) +
geom_histogram(bins = 15) +
facet_wrap(~year)
# line plot for all the countries.
gapminder %>%
ggplot(aes(x = year, y = lifeExp, by = country)) +
ggtitle(label = "Time plot for Life Expectancy for all the Countries.") +
geom_line() +
theme(plot.title = element_text(size = 20, face = "bold"))
## Create a line plot of life expectancy values for the countries. Set the color for the continent values In the aesthetics, you need to set x,y, by and color.
gapminder %>%
ggplot(aes(x = year, y = lifeExp, by = country, color = country)) +
ggtitle(label = "Time plot for Life Expectancy for all the Countries.") +
geom_line(show.legend = F) +
theme(plot.title = element_text(size = 15, face = "bold"))
## Create a line plot of life expectancy values for the countries. create the facets for the continent values
gapminder %>%
ggplot(aes(x = year, y = lifeExp, by = country, color = country)) +
ggtitle(label = "Time plot for Life Expectancy for all the Countries.") +
geom_line(show.legend = F) +
theme(plot.title = element_text(size = 20, face = "bold")) +
facet_wrap(~continent)