library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.2.1
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.7 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.4.0
## ✔ readr 2.1.2 ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(socviz)
## Warning: package 'socviz' was built under R version 4.2.1
library(gapminder)
## Warning: package 'gapminder' was built under R version 4.2.1
#Metadata of mpg using Summary()
metadata_mpg <- summary(mpg)
metadata_mpg
## manufacturer model displ year
## Length:234 Length:234 Min. :1.600 Min. :1999
## Class :character Class :character 1st Qu.:2.400 1st Qu.:1999
## Mode :character Mode :character Median :3.300 Median :2004
## Mean :3.472 Mean :2004
## 3rd Qu.:4.600 3rd Qu.:2008
## Max. :7.000 Max. :2008
## cyl trans drv cty
## Min. :4.000 Length:234 Length:234 Min. : 9.00
## 1st Qu.:4.000 Class :character Class :character 1st Qu.:14.00
## Median :6.000 Mode :character Mode :character Median :17.00
## Mean :5.889 Mean :16.86
## 3rd Qu.:8.000 3rd Qu.:19.00
## Max. :8.000 Max. :35.00
## hwy fl class
## Min. :12.00 Length:234 Length:234
## 1st Qu.:18.00 Class :character Class :character
## Median :24.00 Mode :character Mode :character
## Mean :23.44
## 3rd Qu.:27.00
## Max. :44.00
#Metadata of gapminder data frame
metadata_gapminder <- summary(gapminder)
metadata_gapminder
## country continent year lifeExp
## Afghanistan: 12 Africa :624 Min. :1952 Min. :23.60
## Albania : 12 Americas:300 1st Qu.:1966 1st Qu.:48.20
## Algeria : 12 Asia :396 Median :1980 Median :60.71
## Angola : 12 Europe :360 Mean :1980 Mean :59.47
## Argentina : 12 Oceania : 24 3rd Qu.:1993 3rd Qu.:70.85
## Australia : 12 Max. :2007 Max. :82.60
## (Other) :1632
## pop gdpPercap
## Min. :6.001e+04 Min. : 241.2
## 1st Qu.:2.794e+06 1st Qu.: 1202.1
## Median :7.024e+06 Median : 3531.8
## Mean :2.960e+07 Mean : 7215.3
## 3rd Qu.:1.959e+07 3rd Qu.: 9325.5
## Max. :1.319e+09 Max. :113523.1
##
#Assigning ggplot to a varible P
p <- ggplot(data = gapminder,
mapping = aes(x = gdpPercap, y = lifeExp ))
p
#Structure of P object
str(p)
## List of 9
## $ data : tibble [1,704 × 6] (S3: tbl_df/tbl/data.frame)
## ..$ country : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
## ..$ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
## ..$ year : int [1:1704] 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
## ..$ lifeExp : num [1:1704] 28.8 30.3 32 34 36.1 ...
## ..$ pop : int [1:1704] 8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
## ..$ gdpPercap: num [1:1704] 779 821 853 836 740 ...
## $ layers : list()
## $ scales :Classes 'ScalesList', 'ggproto', 'gg' <ggproto object: Class ScalesList, gg>
## add: function
## clone: function
## find: function
## get_scales: function
## has_scale: function
## input: function
## n: function
## non_position_scales: function
## scales: NULL
## super: <ggproto object: Class ScalesList, gg>
## $ mapping :List of 2
## ..$ x: language ~gdpPercap
## .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
## ..$ y: language ~lifeExp
## .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
## ..- attr(*, "class")= chr "uneval"
## $ theme : list()
## $ coordinates:Classes 'CoordCartesian', 'Coord', 'ggproto', 'gg' <ggproto object: Class CoordCartesian, Coord, gg>
## aspect: function
## backtransform_range: function
## clip: on
## default: TRUE
## distance: function
## expand: TRUE
## is_free: function
## is_linear: function
## labels: function
## limits: list
## modify_scales: function
## range: function
## render_axis_h: function
## render_axis_v: function
## render_bg: function
## render_fg: function
## setup_data: function
## setup_layout: function
## setup_panel_guides: function
## setup_panel_params: function
## setup_params: function
## train_panel_guides: function
## transform: function
## super: <ggproto object: Class CoordCartesian, Coord, gg>
## $ facet :Classes 'FacetNull', 'Facet', 'ggproto', 'gg' <ggproto object: Class FacetNull, Facet, gg>
## compute_layout: function
## draw_back: function
## draw_front: function
## draw_labels: function
## draw_panels: function
## finish_data: function
## init_scales: function
## map_data: function
## params: list
## setup_data: function
## setup_params: function
## shrink: TRUE
## train_scales: function
## vars: function
## super: <ggproto object: Class FacetNull, Facet, gg>
## $ plot_env :<environment: R_GlobalEnv>
## $ labels :List of 2
## ..$ x: chr "gdpPercap"
## ..$ y: chr "lifeExp"
## - attr(*, "class")= chr [1:2] "gg" "ggplot"
#adding geom_point() to p
p + geom_point()
#Replacing geom_point() with geom_smooth()
p + geom_smooth()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
#Adding both geom_point and geom_smooth
p + geom_point()+geom_smooth()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
#Adding linear element to geom_smooth()
p + geom_point()+geom_smooth(method = "lm")
## `geom_smooth()` using formula 'y ~ x'
#Changing the x-axis scare to log 10
p + geom_point() + geom_smooth(method = "lm") + scale_x_log10()
## `geom_smooth()` using formula 'y ~ x'
#changing the y-axis scale to log 10
p + geom_point() + geom_smooth(method = "lm") + scale_y_log10()
## `geom_smooth()` using formula 'y ~ x'
#changing from lm to gam
p + geom_point() + geom_smooth(method = "gam") + scale_y_log10()
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'
#Replacing the scientific notification on x-axis with dollar signs
p + geom_point() + geom_smooth(method = "gam") + scale_x_log10(labels = scales ::dollar)
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'
#Continent each point with color
p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp,
color = continent))
p + geom_point() + geom_smooth(method = "gam") + scale_x_log10()
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'
#adding labels to the plot p
p + geom_point(alpha = 0.3) +
geom_smooth(method = "gam") +
scale_x_log10(labels = scales::dollar) +
labs(x = "GDP Per Capita", y = "Life Expectancy in Years",
title = "Economic Growth and Life Expectancy",
subtitle = "Data points are country-years",
caption = "Source: Gapminder.")
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'
#Changing the method to loess
p + geom_point(alpha = 0.3) +
geom_smooth(method = "loess") +
scale_x_log10(labels = scales::dollar) +
labs(x = "GDP Per Capita", y = "Life Expectancy in Years",
title = "Economic Growth and Life Expectancy",
subtitle = "Data points are country-years",
caption = "Source: Gapminder.")
## `geom_smooth()` using formula 'y ~ x'
#Using method fill to change the appearance of lines
p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp,
color = continent, fill = continent))
p + geom_point() + geom_smooth(method = "loess") + scale_x_log10()
## `geom_smooth()` using formula 'y ~ x'
#Changing the figure size to 8X5
knitr::opts_chunk$set(fig.width = 8, fig.height = 5)
#Saving one of the plot
ggsave(filename = "my_figure.png")
## Saving 8 x 5 in image
## `geom_smooth()` using formula 'y ~ x'
#Saving one of the plot in different format and different location
ggsave(filename = "my_figure.pdf")
## Saving 8 x 5 in image
## `geom_smooth()` using formula 'y ~ x'
p_out <- p + geom_point() + geom_smooth(method = "loess") + scale_x_log10()
ggsave("my_figure.pdf", plot = p_out)
## Saving 8 x 5 in image
## `geom_smooth()` using formula 'y ~ x'
#Mapping different attributes
ggsave(filename = "my_figure.pdf")
## Saving 8 x 5 in image
## `geom_smooth()` using formula 'y ~ x'
p_out <- p + geom_point() + geom_smooth(method = "loess") + scale_x_log10()
ggsave("my_figure.pdf", plot = p_out,height = 8, width = 10, units = "in")
## `geom_smooth()` using formula 'y ~ x'