codes

Case 1

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ 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(ggplot2)
library(lattice)
employees_info <- data.frame( Respective_Sectors <- c("Health", "Retail", "Accom & Food", "Admin & Waste",                         "Mfg.", "Prof. Serv.", "Constr.", "Finance",                          "Wholesale", "Transp.", "Other Serv.", "Edu.",                          "Co. Mgmt.", "Info.", "Real Est.", "Arts & Rec.",                          "Util.", "Mining & Oil", "Agri."),   Avg_Employees_In_Mlns = c(21.2, 15.9, 13.8, 13.6, 12.2, 10.2, 7.4, 6.8, 6.1, 6.1, 5.5, 3.7, 3.7, 3.6, 2.3, 2.3, 0.6, 0.5, 0.2) )

print(employees_info)
   Respective_Sectors....c..Health....Retail....Accom...Food....Admin...Waste...
1                                                                         Health
2                                                                         Retail
3                                                                   Accom & Food
4                                                                  Admin & Waste
5                                                                           Mfg.
6                                                                    Prof. Serv.
7                                                                        Constr.
8                                                                        Finance
9                                                                      Wholesale
10                                                                       Transp.
11                                                                   Other Serv.
12                                                                          Edu.
13                                                                     Co. Mgmt.
14                                                                         Info.
15                                                                     Real Est.
16                                                                   Arts & Rec.
17                                                                         Util.
18                                                                  Mining & Oil
19                                                                         Agri.
   Avg_Employees_In_Mlns
1                   21.2
2                   15.9
3                   13.8
4                   13.6
5                   12.2
6                   10.2
7                    7.4
8                    6.8
9                    6.1
10                   6.1
11                   5.5
12                   3.7
13                   3.7
14                   3.6
15                   2.3
16                   2.3
17                   0.6
18                   0.5
19                   0.2
payroll_info <- data.frame(   Respective_Sectors <- c("Health", "Retail", "Accom & Food", "Admin & Waste",                            "Mfg.", "Prof. Serv.", "Constr.", "Finance",                            "Wholesale", "Transp.", "Other Serv.", "Edu.",                            "Co. Mgmt.", "Info.", "Real Est.", "Arts & Rec.",                            "Util.", "Mining & Oil", "Agri."),   Avg_Payroll_In_Dollars = c(60435, 36685, 25598, 56285, 69846,                                105185, 72589, 123705, 87120, 56557,                                41702, 48193, 126844, 141904, 69496,                                47463, 121998, 109364, 52905)   )  
print(payroll_info)
   Respective_Sectors....c..Health....Retail....Accom...Food....Admin...Waste...
1                                                                         Health
2                                                                         Retail
3                                                                   Accom & Food
4                                                                  Admin & Waste
5                                                                           Mfg.
6                                                                    Prof. Serv.
7                                                                        Constr.
8                                                                        Finance
9                                                                      Wholesale
10                                                                       Transp.
11                                                                   Other Serv.
12                                                                          Edu.
13                                                                     Co. Mgmt.
14                                                                         Info.
15                                                                     Real Est.
16                                                                   Arts & Rec.
17                                                                         Util.
18                                                                  Mining & Oil
19                                                                         Agri.
   Avg_Payroll_In_Dollars
1                   60435
2                   36685
3                   25598
4                   56285
5                   69846
6                  105185
7                   72589
8                  123705
9                   87120
10                  56557
11                  41702
12                  48193
13                 126844
14                 141904
15                  69496
16                  47463
17                 121998
18                 109364
19                  52905
colnames(employees_info)[1] <- "Respective_Sectors" 
colnames(payroll_info)[1] <- "Respective_Sectors"  
merging_info <- merge(employees_info, payroll_info, by = "Respective_Sectors")
ggplot(merging_info, aes(x = Avg_Employees_In_Mlns, y = Avg_Payroll_In_Dollars)) +   
  geom_point(color = 'darkgreen', size = 3) +   
  geom_smooth(method = "lm", linetype = "dashed", color = "black") +   
  labs(title = "Average Payroll per Employee vs. Average Number of Employees",
       x = "Average Number of Employees (in Millions)",        
       y = "Average Payroll per Employee (in $)") +   
  scale_y_continuous(labels = scales::comma) +  # Adds commas for better readability on Y-axis   
  scale_x_continuous(labels = scales::comma) +  # Adds commas for better readability on X-axis   
  theme(plot.title = element_text(hjust = 0.5, size = 14, face = "bold"),
        axis.title.x = element_text(size = 12),
        axis.title.y = element_text(size = 12),
        axis.text = element_text(size = 10),
        plot.margin = margin(1, 1, 1, 1, "cm"))
`geom_smooth()` using formula = 'y ~ x'

heat_map_information <- merging_info %>%
  pivot_longer(cols = c("Avg_Employees_In_Mlns", "Avg_Payroll_In_Dollars"),
               names_to = "Metrics",
               values_to = "Value")

ggplot(heat_map_information, aes(x = Respective_Sectors, y = Metrics, fill = Value)) + 
  geom_tile(color = "white") + 
  scale_fill_gradient(low = "lightblue", high = "darkblue") + 
  coord_flip() +  # Flip coordinates for better readability 
  labs(title = "Heatmap of Average Employees and Payroll by Sector",
       x = "Respective Sectors",
       y = "Metrics",
       fill = "Value",
       caption = "Source: U.S. Census Bureau") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        plot.title = element_text(hjust = 0.5),
        panel.grid.major.y = element_blank())

Case 2

hw for Theme

# hw for theme
library(ggplot2)
hw <- theme_gray()+ theme(
  plot.title=element_text(hjust=0.5),
  plot.subtitle=element_text(hjust=0.5),
  plot.caption=element_text(hjust=-.5),
  
  strip.text.y = element_blank(),
  strip.background=element_rect(fill=rgb(.9,.95,1),
                                colour=gray(.5), linewidth =.2),
  
  panel.border=element_rect(fill=FALSE,colour=gray(.70)),
  panel.grid.minor.y = element_blank(),
  panel.grid.minor.x = element_blank(),
  panel.spacing.x = unit(0.10,"cm"),
  panel.spacing.y = unit(0.05,"cm"),
  
  # axis.ticks.y= element_blank()
  axis.ticks=element_blank(),
  axis.text=element_text(colour="black"),
  axis.text.y=element_text(margin=margin(0,3,0,3)),
  axis.text.x=element_text(margin=margin(-1,0,3,0))
)

Loading data

# Load necessary libraries
library(ggplot2)
library(tidyr)
source('hw.R')

# Assuming your imported data frame is named fs(Federal spending)
# Create the data frame (example, adjust as necessary)
fs <- data.frame(
  Year = c(1966, 1975, 1985, 1995, 2005, 2015),
  Interest = c(7, 7, 13.7, 15.3, 7.4, 6.1),
  Medicare = c(0, 3.9, 6.6, 10.5, 11.9, 14.5),
  Medicaid = c(0, 2.1, 2.4, 5.9, 7.4, 9.5),
  Social_Security = c(15.1, 19.1, 19.7, 22, 21, 23.9),
  Income_Security = c(3.8, 8.7, 5.5, 7.7, 8, 8.1),
  Other_Mandatory = c(6.6, 11.7, 8.2, 2.7, 5.1, 6.3),
  Military_Defense = c(43.9, 26.4, 26.7, 18, 20, 15.8),
  Other_Domestic = c(19, 19, 15.5, 16.8, 17.8, 14.7),
  Other_Intl = c(4.2, 2.1, 1.7, 1.1, 1.4, 1.2)
)

print(fs)
  Year Interest Medicare Medicaid Social_Security Income_Security
1 1966      7.0      0.0      0.0            15.1             3.8
2 1975      7.0      3.9      2.1            19.1             8.7
3 1985     13.7      6.6      2.4            19.7             5.5
4 1995     15.3     10.5      5.9            22.0             7.7
5 2005      7.4     11.9      7.4            21.0             8.0
6 2015      6.1     14.5      9.5            23.9             8.1
  Other_Mandatory Military_Defense Other_Domestic Other_Intl
1             6.6             43.9           19.0        4.2
2            11.7             26.4           19.0        2.1
3             8.2             26.7           15.5        1.7
4             2.7             18.0           16.8        1.1
5             5.1             20.0           17.8        1.4
6             6.3             15.8           14.7        1.2

Reshaping the data

# Reshape the data to long format
fs_long <- pivot_longer(fs, cols = -Year, names_to = "Topic", values_to = "Percentage")

print(fs_long)
# A tibble: 54 × 3
    Year Topic            Percentage
   <dbl> <chr>                 <dbl>
 1  1966 Interest                7  
 2  1966 Medicare                0  
 3  1966 Medicaid                0  
 4  1966 Social_Security        15.1
 5  1966 Income_Security         3.8
 6  1966 Other_Mandatory         6.6
 7  1966 Military_Defense       43.9
 8  1966 Other_Domestic         19  
 9  1966 Other_Intl              4.2
10  1975 Interest                7  
# ℹ 44 more rows

Plot the data in Line Graph

# Plot the data in Line Graph
  ggplot(fs_long, aes(x = Year, y = Percentage, color = Topic)) +
    geom_line(size = 1) +
    geom_point(size = 2) +
    labs(title = "Trends in Federal Spending",
         x = "Year",
         y = "Percentage") +hw
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.

Plot the data in Scatter plot

# Plot the data in Scatter plot
ggplot(fs_long, aes(x = Year, y = Percentage, color = Topic)) +
  geom_point(size = 3) +
  labs(title = "Trends in Federal Spendings",
       x = "Year", y = "Percentage") + hw