1. Download the dataframe credit.csv from http://nathanieldphillips.com/wp-content/uploads/2015/05/credit.txt. The data are stored in a comma-separated text file with headers. Load the dataframe into an object called credit.
credit <- read.table(file = "http://nathanieldphillips.com/wp-content/uploads/2015/05/credit.txt", header = T, sep = ",", stringsAsFactors = F) 
  1. Use the column amount to create the following histogram:

Step 1: Create the histogram. Step 2: Add the vertical line at the median using abline() or segments(). Step 3: Create the text to be added using paste(). Step 4: Add the text using text()

#Step 1: 
hist(x = credit$amount, 
     main = "Personal loans by German borrowers", 
     xlab = "Loan size (in DM)", 
     ylab = "Frequency", 
     xlim = c(0, 20000), ylim = c(0, 500))

#Step 2: 
abline(v = 2319.5, lwd = 2, lty = 2)

#Step 3:
paste("Median = ", median(credit$amount), sep = "")
## [1] "Median = 2319.5"
#Step 4:
text(x = 6000, y = 400, 
     labels = paste("Median = ", median(credit$amount), sep = "") )

3.Using the columns age and amount, to create the following scatterplot

plot(x = credit$age,
     y = credit$amount, 
     main = "Borrower age and loan amount", 
     ylab = "Loan amount (in DM)", 
     xlab = "Borrower Age", 
     xlim = c(20, 70), 
     ylim = c(0, 15000),
     pch = 16,
     col = "gray" 
     )

  1. Using the columns years_at_residence and amount, create the following beanplots using the beanplot package.
#install.packages("beanplot")
library("beanplot")

beanplot(amount ~ years_at_residence,
  data = credit,
  main = "Number of years at residence and loan amount", 
  xlab = "Years at residence", 
  ylab = "loan amount in CM (log-transformed)", 
  col = "white",
  lwd = 1,
  what = c(1, 1, 1, 1), log = "y")

5 Using the columns job, months_loan_duration and amount Create this plot: Step 1: Create a blank plot Step 2: Add gridlines with abline() Step 3: Add red points for Skilled workers with points() Step 4: Add blue points for Unskilled workers with points() Step 5: Add legend with legend()

#Step 1: 
plot(x = 1, y = 1, 
     xlab = "Loan duration (in months)", 
     ylab = "Loan amount (in DM)", 
     type = "n",
     xlim = c(0, 80), 
     ylim = c(0, 15000), 
     main = "Loan duration and amount of skilled and unskilled borrowers")

#Step 2:
# Add horizontal gridlines 
abline(h = seq(0, 15000, 1000), lwd = 1, col = "gray") 
abline(h = seq(0, 15000, 5000), lwd = 2, col = "gray") 

# Add vertical gridlines
abline(v = seq(0, 80, 10), lwd = 1, col = "gray")
abline(v = seq(0, 80, 20), lwd = 2, col = "gray")

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.