Using ggplot2 to create segmented boxplot

I am using ggplot2 library to produce box plots for AccountBalance, MonthlyExpenses, CreditScore and Age segmented by approval (variable “Approved”). I have not had to segment a box plot and am coming up short when researching the topic. These are the list of variables listed in R:

crx$AccountBalance <- as.numeric(crx$AccountBalance)
crx$MonthlyExpenses <- as.integer(crx$MonthlyExpenses)
crx$CreditScore <- as.numeric(crx$CreditScore) 
crx$Age <- as.numeric(crx$Age)
crx$Approved <- as.factor(crx$Approved)

I have created the box plots but don't know how to segment by approval (variable “Approved”). Could some one please advise me? Here are my box plot codes:

#AccountBalance Boxplot
AB <- crx$AccountBalance
boxplot(AB)

#MonthlyExpenses Boxplot
ME <- crx$MonthlyExpenses
boxplot(ME)

#CreditScore Boxplot
CS <- crx$CreditScore
boxplot(CS)

#Age Boxplot                    
Age <- crx$Age
boxplot(Age)

Thank you for your support, it's been a tremendous help

Is this the kind of thing you are trying to do?

library(ggplot2)
#Invent some data
df <- data.frame(AccountBalance = rnorm(50, 50, 4),
                 MonthlyExpenses = runif(50, 10, 40),
                 Approved = sample(c("Y", "N"), size = 50, replace = TRUE))
head(df)
#>   AccountBalance MonthlyExpenses Approved
#> 1       50.91404        11.42858        Y
#> 2       46.21259        10.91639        N
#> 3       52.23401        39.89171        Y
#> 4       51.53475        25.11372        Y
#> 5       51.54027        16.21440        N
#> 6       45.44255        26.48957        N

#with Base boxplot
boxplot(formula = AccountBalance ~ Approved, data = df)


#with ggplot
ggplot(df, aes(Approved, AccountBalance)) + geom_boxplot()

Created on 2019-05-24 by the reprex package (v0.2.1)

1 Like

Fabulous! Thank you very much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.