———————————————–

1. Install and Load ggplot2

———————————————–

# Load the library
library(ggplot2)

———————————————–

Set a working Directory

———————————————–

setwd("C:/Users/Admin/Desktop/R TRAINING")

Import dataset

gss <-read.csv("GSSsubset.csv")

———————————————–

2. Creating Basic Plot

———————————————–

Scatter Plot***Visualize the relationship between two continuous variable

Example: Scatter plot of ‘income’ vs ‘age’

ggplot(gss, aes(x = age, y = income)) +
  geom_point(color = "blue") +
  labs(title = "Income vs Age", x = "Age", y = "Income") +
  theme_minimal()

# Bar Plot***Display the count or summary of categorical data. # ———————————————– # Example: Bar plot of ‘gender’ counts

ggplot(gss, aes(x = sex)) +
  geom_bar(fill = "green") +
  labs(title = "Gender Distribution", x = "Gender", y = "Count") +
  theme_classic()

# ———————————————– #Histogram***Show the distribution of a single variable. # ———————————————– # Example: Histogram of ‘income’

ggplot(gss, aes(x = income)) +
  geom_histogram(binwidth = 10000, fill = "orange", color = "black") +
  labs(title = "Income Distribution", x = "Income", y = "Frequency") +
  theme_light()

# ———————————————– #Boxplot***Summarize the distribution of a continuous variable across categories. # ———————————————– # Example: Boxplot of ‘income’ by ‘gender’

ggplot(gss, aes(x = sex, y = income)) +
  geom_boxplot(fill = "purple") +
  labs(title = "Income by Gender", x = "Gender", y = "Income") +
  theme_bw()

# ———————————————– # 3. Customizing Plots # ———————————————– # Adding Titles and Labels***Customize the title, axis labels, and legend. # ———————————————– # Example: Scatter plot with customizations

ggplot(gss, aes(x = age, y = income, color = sex)) +
  geom_point(size = 3) +
  labs(title = "Income vs Age by Gender",
       x = "Age (years)",
       y = "Income ($)",
       color = "Gender") +
  theme_minimal()

# ———————————————– # Changing Themes***Use different themes for better aesthetics. # ———————————————– # Example: Scatter plot with a dark theme

ggplot(gss, aes(x = age, y = income)) +
  geom_point(color = "tomato") +
  labs(title = "Income vs Age", 
       x = "Age", 
       y = "Income") +
  theme_classic()

# ———————————————– # Modifying Colors, Shapes, and Sizes # Enhance visualizations by modifying aesthetics. # ———————————————– # Example: Scatter plot with customized colors and sizes

ggplot(gss, aes(x = age, y = income, color = sex, size = income)) +
  geom_point(alpha = 0.6) +
  scale_color_manual(values = c("red", "blue")) +
  labs(title = "Income vs Age by Gender", x = "Age", y = "Income") +
  theme_classic()

View(gss)

#load the library

library(ggplot2)
library(devtools)
## Loading required package: usethis
library(predict3d)
library(psych)
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(gtsummary)
library(DescTools)
## 
## Attaching package: 'DescTools'
## The following objects are masked from 'package:psych':
## 
##     AUC, ICC, SD
library(nortest) 
library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(sandwich)
library(patchwork)

set a working directory

setwd("C:/Users/Admin/Desktop/R TRAINING")

3. Data visualization with ggplot2

scatter plot of ‘income’ vs’ age

ggplot(data = gss)

ggplot(data = gss, aes(x = age, y = income))

#The geometries

ggplot(data = gss, aes(x = age, y = income)) + geom_point()

# Change the color of points to my choice

ggplot(data = gss, aes(x = age, y = income)) + geom_point(color = "red")

# Color point in the plot by sex ***The color differentiates income level of the two genders.

ggplot(data = gss, aes(x = age, y = income,colour = sex)) + geom_point() 

#Set the theme at the end of your plot****Theme changes the background of your graph

ggplot(data = gss, aes(x = age, y = income,colour = sex)) + geom_point() +theme_grey()

###### Labeling ## Lables

ggplot(data = gss, aes(x = age, y = income,colour = sex)) + geom_point()+
  theme_grey() +labs(title = "Gender Distribution", 
                     x= "Gender" ,
                     y = "Number of respondents",caption="Mbuvi Data Analyst Cdam") 

#Add caption

ggplot(data = gss, aes(x = age, y = income,colour = sex)) + geom_point()+
  theme_grey() +labs(title = "Gender distribution", 
                     x= "Gender" ,
                     y = "Number of respondents" ,
                     caption="Mbuvi Data Analyst Cdam")+ theme_classic()+theme((plot.title = element_text(hjust = 0.5)))+ 
  facet_wrap(~degree)

# Bar plots of’Gender’ counts

ggplot(data = gss, aes(x = sex)) + geom_bar(fill = "red")

  labs(title = "Gender Distribution", 
       x = "Gender",
       y = "Number of responden") +
  theme_classic()
## NULL

#—————————————————————————- #### ## Histogram ** show the distribution of a single variable.

#————————————————————–

Histogram of Incomne

ggplot(gss, aes(x = income)) + geom_histogram(bins = 20,fill = "blue", color="black")+
       labs(title = "Gender Distribution", 
                     x = "Gender",
                    y = "Number of responden") +
       theme_classic()  

#Box plot

ggplot(gss, aes(x = sex, y= income)) + geom_boxplot(fill = "purple",)+
    labs(title = "income by Gender", 
         x = "Gender",
         y = "Income") +
    theme_bw()

Combine the plots

library(patchwork)