For each exercise below, show code. Once you’ve completed things, don’t forget to input everything into the quiz on Canvas and to upload this document (knitted version please!) at the end of the quiz. A few tips:
install.packages() and load them using
library().library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── 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(gapminder)
data(gapminder)
lifeExp)
in three countries of your choosing using a line plot
(geom_line()).gapminder
## # A tibble: 1,704 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
## 7 Afghanistan Asia 1982 39.9 12881816 978.
## 8 Afghanistan Asia 1987 40.8 13867957 852.
## 9 Afghanistan Asia 1992 41.7 16317921 649.
## 10 Afghanistan Asia 1997 41.8 22227415 635.
## # ℹ 1,694 more rows
ggplot(gapminder, aes(x=`year`, y = `lifeExp`, colour = country)) %>%
+ geom_line(data = filter(gapminder, country == "United States")) %>%
+ geom_line(data = filter(gapminder, country == "Mexico")) %>%
+ geom_line(data = filter(gapminder, country == "Canada")) %>%
+ theme_classic()+ labs(x="Year", y= "Life Expectancy", title ="North American Life Expectancy from 1952-2007")+scale_colour_discrete(name = "Country")
# Using ggplot, I started by specifying the x and y variables and telling R to associate each country with a different color. The next three lines are individually filtering each country I wanted to plot. For this exercise, I chose to do the three main countries in North America. The last line is more for cosmetic purposes. Here, I assigned labels for each axis, gave the graph and legend a clear title.
geom_boxplot()) to
visualize differences in GDP per capita across continents.ggplot(gapminder, aes(x=continent, y=gdpPercap))+geom_boxplot() + coord_cartesian(ylim = c(0,40000)) + labs(x= "Continent", y="GDP Per Capita", title ="GDP per Capita Across Each Continent")
#To create a box and whisker plot, I used a similar method used in Question 1. I started by associating the x and y axis with the continent and GDP variables. I then specified that I wanted this data to be plotted on a boxplot. Originally, I plotted this data with no y value limit but the outliers made it tricky to visualize the differences between countries. This said, I set a limit of 40,000. Lastly, I added both x and y labels along with a title.
geom_point()) showing the
relationship between GDP per capita and life expectancy in 2007.ggplot(gapminder, aes(x=lifeExp, y=gdpPercap))+geom_point(data = filter(gapminder, year == 2007))+ labs(x= "Life Expectancy (years)", y="GDP Per capita", title= "Relationship between GPD Per Capita and Life Expectancy in 2007")
#Using the ggplot, I started again by specifying the x and y data. I then used a filter to only show the data from 2007. Lastly, I added labels and a title.