-4

I have following data:

loan_amnt   term      grade sub_grade home_ownership  verification_status  d_status
7000        60 months C     C5        RENT            Not Verified         Not Defaulted
6500        60 months C     C3        OWN             Not Verified         Not Defaulted
12000       36 months B     B5        OWN             Verified             Defaulted
9000        36 months C     C1        RENT            Verified             Not Defaulted
1000        36 months D     D1        RENT            Not Verified         Defaulted

I required multivariate bar chart in which I wan to have d_status filled inside and y axis have percentage of value. In x axis I want term, grade, and verification_status. It means I will required 3 stacked bar charts together.

How can I plot using ggplot? Please help.

CKE
  • 1,533
  • 19
  • 18
  • 29
  • 2
    So I down-voted your question for it shows no effort and also you forgot to say, "please" (deliberate pun intended). There are no "free lunches". Okay, here is the thing, you want answer's, then you got to show some effort. What have you tried so far? What does the data look like, what are the types of variable etc. See here on how to create a [minimum reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – mnm Jun 30 '18 at 05:18

1 Answers1

0

You can use gridExtra library.

df <- read.table(text = "loan_amnt   term      grade sub_grade home_ownership  verification_status  d_status
7000        60_months C     C5        RENT            Not_Verified         Not_Defaulted
6500        60_months C     C3        OWN             Not_Verified         Not_Defaulted
12000       36_months B     B5        OWN             Verified             Defaulted
9000        36_months C     C1        RENT            Verified             Not_Defaulted
1000        36_months D     D1        RENT            Not_Verified         Defaulted", 
                 header = TRUE)

library(ggplot2)
library(scales)
g1 <- ggplot(df, aes(x = term, fill = d_status)) +
  geom_bar(aes(y = (..count..)/sum(..count..))) + 
  scale_y_continuous(labels = percent) + 
  ylab("") +
  theme(strip.text.x = element_blank(),
      legend.position=c(0.85, 0.9))

g2 <- ggplot(df, aes(x = grade, fill = d_status)) +
  geom_bar(aes(y = (..count..)/sum(..count..))) + 
  scale_y_continuous(labels = percent) + 
  ylab("")  +
  theme(strip.text.x = element_blank(),
        legend.position=c(0.85, 0.9))


g3 <- ggplot(df, aes(x = verification_status, fill = d_status)) +
  geom_bar(aes(y = (..count..)/sum(..count..))) + 
  scale_y_continuous(labels = percent) + 
  ylab("") +
  theme(strip.text.x = element_blank(),
        legend.position=c(0.85, 0.9))


library(gridExtra)
grid.arrange(g1, g2, g3, nrow = 1)

Result is: Stacked bars with percentage

Artem
  • 3,304
  • 3
  • 18
  • 41