Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
Running Code
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.4
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.4.4 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ 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
# A tibble: 12 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 United States Americas 1952 68.4 157553000 13990.
2 United States Americas 1957 69.5 171984000 14847.
3 United States Americas 1962 70.2 186538000 16173.
4 United States Americas 1967 70.8 198712000 19530.
5 United States Americas 1972 71.3 209896000 21806.
6 United States Americas 1977 73.4 220239000 24073.
7 United States Americas 1982 74.6 232187835 25010.
8 United States Americas 1987 75.0 242803533 29884.
9 United States Americas 1992 76.1 256894189 32004.
10 United States Americas 1997 76.8 272911760 35767.
11 United States Americas 2002 77.3 287675526 39097.
12 United States Americas 2007 78.2 301139947 42952.
gamp_usa_2007 <- gapminder %>%filter (year ==2007, country =="United States")gamp_usa_2007
# A tibble: 1 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 United States Americas 2007 78.2 301139947 42952.
gamp_china_2002 <- gapminder %>%filter (year ==2002, country =="China")
# The Arrange verpgapminder %>%arrange(desc(gdpPercap))
# A tibble: 1,704 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 Kuwait Asia 1957 58.0 212846 113523.
2 Kuwait Asia 1972 67.7 841934 109348.
3 Kuwait Asia 1952 55.6 160000 108382.
4 Kuwait Asia 1962 60.5 358266 95458.
5 Kuwait Asia 1967 64.6 575003 80895.
6 Kuwait Asia 1977 69.3 1140357 59265.
7 Norway Europe 2007 80.2 4627926 49357.
8 Kuwait Asia 2007 77.6 2505559 47307.
9 Singapore Asia 2007 80.0 4553009 47143.
10 Norway Europe 2002 79.0 4535591 44684.
# ℹ 1,694 more rows
# A tibble: 142 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 Norway Europe 2007 80.2 4627926 49357.
2 Kuwait Asia 2007 77.6 2505559 47307.
3 Singapore Asia 2007 80.0 4553009 47143.
4 United States Americas 2007 78.2 301139947 42952.
5 Ireland Europe 2007 78.9 4109086 40676.
6 Hong Kong, China Asia 2007 82.2 6980412 39725.
7 Switzerland Europe 2007 81.7 7554661 37506.
8 Netherlands Europe 2007 79.8 16570613 36798.
9 Canada Americas 2007 80.7 33390141 36319.
10 Iceland Europe 2007 81.8 301931 36181.
# ℹ 132 more rows
gapminder %>%arrange(desc(lifeExp))
# A tibble: 1,704 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 Japan Asia 2007 82.6 127467972 31656.
2 Hong Kong, China Asia 2007 82.2 6980412 39725.
3 Japan Asia 2002 82 127065841 28605.
4 Iceland Europe 2007 81.8 301931 36181.
5 Switzerland Europe 2007 81.7 7554661 37506.
6 Hong Kong, China Asia 2002 81.5 6762476 30209.
7 Australia Oceania 2007 81.2 20434176 34435.
8 Spain Europe 2007 80.9 40448191 28821.
9 Sweden Europe 2007 80.9 9031088 33860.
10 Israel Asia 2007 80.7 6426679 25523.
# ℹ 1,694 more rows
# The mutate verpgapminder %>%mutate(pop = pop /1000000) %>%arrange (desc(pop))
# A tibble: 1,704 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <dbl> <dbl>
1 China Asia 2007 73.0 1319. 4959.
2 China Asia 2002 72.0 1280. 3119.
3 China Asia 1997 70.4 1230. 2289.
4 China Asia 1992 68.7 1165. 1656.
5 India Asia 2007 64.7 1110. 2452.
6 China Asia 1987 67.3 1084. 1379.
7 India Asia 2002 62.9 1034. 1747.
8 China Asia 1982 65.5 1000. 962.
9 India Asia 1997 61.8 959 1459.
10 China Asia 1977 64.0 943. 741.
# ℹ 1,694 more rows
# Jdp per capita by popgapminder %>%mutate (gdp = gdpPercap * pop) %>%filter(year ==2007)%>%arrange (desc(gdp))
# A tibble: 142 × 7
country continent year lifeExp pop gdpPercap gdp
<fct> <fct> <int> <dbl> <int> <dbl> <dbl>
1 United States Americas 2007 78.2 301139947 42952. 1.29e13
2 China Asia 2007 73.0 1318683096 4959. 6.54e12
3 Japan Asia 2007 82.6 127467972 31656. 4.04e12
4 India Asia 2007 64.7 1110396331 2452. 2.72e12
5 Germany Europe 2007 79.4 82400996 32170. 2.65e12
6 United Kingdom Europe 2007 79.4 60776238 33203. 2.02e12
7 France Europe 2007 80.7 61083916 30470. 1.86e12
8 Brazil Americas 2007 72.4 190010647 9066. 1.72e12
9 Italy Europe 2007 80.5 58147733 28570. 1.66e12
10 Mexico Americas 2007 76.2 108700891 11978. 1.30e12
# ℹ 132 more rows
# life expectancy by monthsgapminder %>%mutate (lifeExp = lifeExp *12)
# A tibble: 1,704 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 Afghanistan Asia 1952 346. 8425333 779.
2 Afghanistan Asia 1957 364. 9240934 821.
3 Afghanistan Asia 1962 384. 10267083 853.
4 Afghanistan Asia 1967 408. 11537966 836.
5 Afghanistan Asia 1972 433. 13079460 740.
6 Afghanistan Asia 1977 461. 14880372 786.
7 Afghanistan Asia 1982 478. 12881816 978.
8 Afghanistan Asia 1987 490. 13867957 852.
9 Afghanistan Asia 1992 500. 16317921 649.
10 Afghanistan Asia 1997 501. 22227415 635.
# ℹ 1,694 more rows
# add a new coulmn for life expectancy in monthsgapminder %>%mutate (lifeExp_months = lifeExp *12)
# A tibble: 1,704 × 7
country continent year lifeExp pop gdpPercap lifeExp_months
<fct> <fct> <int> <dbl> <int> <dbl> <dbl>
1 Afghanistan Asia 1952 28.8 8425333 779. 346.
2 Afghanistan Asia 1957 30.3 9240934 821. 364.
3 Afghanistan Asia 1962 32.0 10267083 853. 384.
4 Afghanistan Asia 1967 34.0 11537966 836. 408.
5 Afghanistan Asia 1972 36.1 13079460 740. 433.
6 Afghanistan Asia 1977 38.4 14880372 786. 461.
7 Afghanistan Asia 1982 39.9 12881816 978. 478.
8 Afghanistan Asia 1987 40.8 13867957 852. 490.
9 Afghanistan Asia 1992 41.7 16317921 649. 500.
10 Afghanistan Asia 1997 41.8 22227415 635. 501.
# ℹ 1,694 more rows
# use filter, murtate and arrangegapminder %>%filter (year ==2007) %>%mutate (lifeExpMopnths = lifeExp *12) %>%arrange (desc(lifeExpMopnths))
# A tibble: 142 × 7
country continent year lifeExp pop gdpPercap lifeExpMopnths
<fct> <fct> <int> <dbl> <int> <dbl> <dbl>
1 Japan Asia 2007 82.6 127467972 31656. 991.
2 Hong Kong, China Asia 2007 82.2 6980412 39725. 986.
3 Iceland Europe 2007 81.8 301931 36181. 981.
4 Switzerland Europe 2007 81.7 7554661 37506. 980.
5 Australia Oceania 2007 81.2 20434176 34435. 975.
6 Spain Europe 2007 80.9 40448191 28821. 971.
7 Sweden Europe 2007 80.9 9031088 33860. 971.
8 Israel Asia 2007 80.7 6426679 25523. 969.
9 France Europe 2007 80.7 61083916 30470. 968.
10 Canada Americas 2007 80.7 33390141 36319. 968.
# ℹ 132 more rows
# Visualizing data with ggplotgapmidner_2007 <- gapminder %>%filter (year ==2007)gapmidner_2007
# A tibble: 142 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 Afghanistan Asia 2007 43.8 31889923 975.
2 Albania Europe 2007 76.4 3600523 5937.
3 Algeria Africa 2007 72.3 33333216 6223.
4 Angola Africa 2007 42.7 12420476 4797.
5 Argentina Americas 2007 75.3 40301927 12779.
6 Australia Oceania 2007 81.2 20434176 34435.
7 Austria Europe 2007 79.8 8199783 36126.
8 Bahrain Asia 2007 75.6 708573 29796.
9 Bangladesh Asia 2007 64.1 150448339 1391.
10 Belgium Europe 2007 79.4 10392226 33693.
# ℹ 132 more rows
library(ggplot2)ggplot (gapminder, aes(x = gdpPercap, y = lifeExp))+geom_point()
gapminder <- gapminderggplot (gapminder, aes(x = gdpPercap, y = lifeExp))+geom_point()+scale_x_log10()
#Log scale for x axisggplot (gamp_2007, aes ( x= gdpPercap, y = lifeExp))+geom_point()+scale_x_log10()
gapminder_1952 <- gapminder %>%filter (year ==1952)ggplot (gapminder_1952, aes (x = lifeExp, y = gdpPercap, color = continent,size = pop))+geom_point()+scale_x_log10()+scale_y_log10()+facet_wrap(~continent)
#groub_by verpmedian_by_years <- gapminder %>%group_by(year) %>%summarize(medianLifeExp =median(lifeExp),max_lifeExp =max(lifeExp),min_lifeExp =min(lifeExp),mean_lifeExp =mean(lifeExp)) %>%arrange(year)ggplot (median_by_years, aes(x = year, y = medianLifeExp, color = year))+geom_point()+geom_line()+labs(title ="Median Life Expectancy by Year",x ="Year",y ="Median Life Expectancy")+theme_minimal()+theme(legend.position ="none")+scale_x_continuous(breaks =seq(1952, 2007, 10))
medina_lifeExp_2007_continat <- gapminder %>%filter (year ==2007) %>%group_by (continent) %>%summarize (medianLifeExp =median(lifeExp),max_lifeExp =max(lifeExp),min_lifeExp =min(lifeExp),mean_lifeExp =mean(lifeExp))ggplot (medina_lifeExp_2007_continat, aes(x = continent, y= medianLifeExp))+geom_col()+labs(title ="Median Life Expectancy by Continent in 2007",x ="Continent",y ="Median Life Expectancy")+theme(legend.position ="none")
# group by year and continentyear_and_continet <- gapminder %>%group_by (year, continent)%>%summarize (medianLifeExp =median(lifeExp),max_lifeExp =max(lifeExp),min_lifeExp =min(lifeExp),mean_lifeExp =mean(lifeExp))
`summarise()` has grouped output by 'year'. You can override using the
`.groups` argument.
ggplot (year_and_continet, aes(x= year, y= medianLifeExp, color = continent))+geom_point(size =3.5)+expand_limits(y =0)+scale_x_log10()+geom_line(size =1.5)+labs(title ="Median Life Expectancy by Year and Continent",x ="Year",y ="Median Life Expectancy")+theme_minimal()+theme(legend.position ="bottom")
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
`summarise()` has grouped output by 'year'. You can override using the
`.groups` argument.
# Plot the change in medianGdpPercap in each continent over timeggplot(by_year_continent, aes (x= year, y= medianLifeExp, color = continent))+geom_point(size =2.5)+theme_minimal()+expand_limits(y =0)