library(ggplot2)
library(gapminder)
str(gapminder)
## 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 ...
summary(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  
## 
head(gapminder)
## # A tibble: 6 × 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.
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
#exploring using various Alpha values
library(ggplot2)

ggplot(data = gapminder, aes(x = lifeExp, fill = continent)) +
  geom_density(alpha = 0.5) +
  labs(title = "Density Plot of Life Expectancy by Continent",
       x = "Life Expectancy",
       y = "Density") +
  theme_minimal()

#exploring using various Alpha values
ggplot(data = gapminder, aes(x = lifeExp, fill = continent)) +
     geom_density(alpha = 0.8) +
    labs(title = "Density Plot of Life Expectancy by Continent",
          x = "Life Expectancy",
         y = "Density") +
   theme_minimal()

#exploring using various Alpha values
ggplot(data = gapminder, aes(x = lifeExp, fill = continent)) +
     geom_density(alpha = 0.2) +
     labs(title = "Density Plot of Life Expectancy by Continent",
          x = "Life Expectancy",
         y = "Density") +
     theme_minimal()

#Explore the relationship between life expectancy and GDP with a scatterplot
ggplot(data =gapminder, aes(x = gdpPercap, y = lifeExp)) +
     geom_point() +
    labs(title = "Scatterplot of Life Expectancy vs. GDP",
         x = "GDP per Capita",
         y = "Life Expectancy") +
    theme_minimal()

#Explore the relationship between life expectancy and GDP with a scatterplot(added colors for better visualization)
ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp,color=year)) +
     geom_point() +
     labs(title = "Scatterplot of Life Expectancy vs. GDP",
          x = "GDP per Capita",
          y = "Life Expectancy") +
    theme_minimal()

#Add scale_x_log10() using the “+” operator.
ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp,color=continent)) +
     geom_point() +scale_x_log10()

     labs(title = "Scatterplot of Life Expectancy vs. GDP",
          x = "GDP per Capita",
          y = "Life Expectancy") +
    theme_minimal()
## NULL
# change the circles to semi-transparent using the alpha variable.
ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
    geom_point(alpha = 0.5) +  # Add alpha for transparency
    scale_x_log10() +
   labs(title = "Scatterplot of Life Expectancy vs. GDP",
         x = "GDP per Capita",
         y = "Life Expectancy") +
    theme_minimal()

#Option 3: customize the theme for the plot.
library(ggplot2)
 
 # Create a custom theme for the plot
 custom_theme <- theme_minimal() +
    theme(
        plot.title = element_text(size = 22, face = "bold"),
        axis.title.x = element_text(size = 12),
        axis.title.y = element_text(size = 14),
         axis.text.x = element_text(size = 15),
        axis.text.y = element_text(size = 13),
        legend.title = element_text(size = 18),
        legend.text = element_text(size = 10)
    )

 # Create the scatterplot with the custom theme
 ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp, color = continent)) +
     geom_point(alpha = 0.5) +
     scale_x_log10() +
    labs(title = "Scatterplot of Life Expectancy vs. GDP",
         x = "GDP per Capita",
         y = "Life Expectancy") +
    custom_theme

library(ggplot2)

# Create a scatterplot of life expectancy vs. GDP with color by continent and alpha
ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp, color = continent)) +
  geom_point(alpha = 0.5) +
  scale_x_log10(
    breaks = c(100, 1000, 10000, 100000),  # Define custom breaks
    labels = scales::comma_format(scale = 0.001)  # Format labels as thousands
  ) +
  labs(title = "Scatterplot of Life Expectancy vs. GDP",
       x = "GDP per Capita",
       y = "Life Expectancy") +
  theme_minimal()

# Try using encode the size of the point by variable “pop”. 
library(ggplot2)


ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp, color = continent, size = pop)) +
  geom_point(alpha = 0.5) +
  scale_x_log10(
    breaks = c(100, 1000, 10000, 100000),  # Define custom breaks
    labels = scales::comma_format(scale = 0.001)  # Format labels as thousands
  ) +
  labs(title = "Scatterplot of Life Expectancy vs. GDP (Size by Population)",
       x = "GDP per Capita",
       y = "Life Expectancy") +
  theme_minimal()