library(readr)


salary <- readr::read_csv("Professorial Salaries.csv")
## Rows: 397 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Rank, Discipline, Sex
## dbl (6): ID, Yrs.since.phd, Yrs.service, NPubs, Ncits, Salary
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(salary)
## # A tibble: 6 × 9
##      ID Rank      Discipline Yrs.since.phd Yrs.service Sex   NPubs Ncits Salary
##   <dbl> <chr>     <chr>              <dbl>       <dbl> <chr> <dbl> <dbl>  <dbl>
## 1     1 Prof      B                     19          18 Male     18    50 139750
## 2     2 Prof      B                     20          16 Male      3    26 173200
## 3     3 AsstProf  B                      4           3 Male      2    50  79750
## 4     4 Prof      B                     45          39 Male     17    34 115000
## 5     5 Prof      B                     40          41 Male     11    41 141500
## 6     6 AssocProf B                      6           6 Male      6    37  97000

q2 yes

library(ggplot2)
ggplot(salary, aes(x = Salary)) + 
  geom_histogram(aes(y = ..density..), binwidth = 5000, fill = "blue", alpha = 0.7) +
  geom_density(col = "red") +
  labs(title = "Distribution of Professors' Salaries", x = "Salary", y = "Density")
## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(density)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

q3

library(ggplot2)
library(gridExtra)
library(grid) 
p = ggplot(data = salary, aes(x
= Salary, fill = Sex))
p1 = p +
geom_histogram(position =
"dodge")
p2 = ggplot(data = salary,
aes(x = Salary, fill = Sex,
color = Sex)) +
geom_density(alpha = 0.1)
grid.arrange(p1, p2, nrow = 2,
top = textGrob("Distribution of
professors' salaries by sex",
gp = gpar(fontsize = 20, font =
1)))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

q4

library(ggplot2)
library(gridExtra)
library(grid)

p = ggplot(data = salary,
aes(x = Rank, fill = Rank, col
= Rank))
p1 = p + geom_bar(position =
"dodge")
salary$Prof.Rank =
factor(salary$Rank, levels =
c("AsstProf", "AssocProf",
"Prof"))
p = ggplot(data = salary,
aes(x = Prof.Rank, fill =
Prof.Rank, col = Prof.Rank))
p2 = p + geom_bar(position =
"dodge")
grid.arrange(p1, p2, nrow = 2,
top = textGrob("Distribution
of professors' ranks", gp =
gpar(fontsize = 20, font =
1)))

p = ggplot(data = salary,
aes(x = Prof.Rank, fill =
Sex, col = Sex))
p1 = p + geom_bar(position =
"dodge")
p1 + ggtitle("Distribution of
professors' rank by sex")

q5

p = ggplot(data = salary,
aes(x = Yrs.service, y =
Salary))
p1 = p + geom_point() +
geom_smooth() + labs(x =
"Time in service (years)", y
= "Professors' salaries
(USD)") +
ggtitle("Correlation between
professors' salaries and time
in service") + theme_bw()
p1
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

p = ggplot(data = salary,
           aes(x = Yrs.service, y = Salary, fill = Sex, col = Sex))
p2 = p + geom_point() +
  geom_smooth(method = "lm", formula = y ~ x + I(x^2) + I(x^3)) +
  labs(x = "Time in service (years)", y = "Professors' salaries (USD)") +
  ggtitle("Correlation between professors' salaries and time in service by sex") +
  theme_bw()

p2