0

I have a table that contains the account balance and marital status of an individual (married, unmarried, divorced). How do I use ggplot() to plot the balance of each marital status at once?

My current method is not very efficient:

married <- subset(bank[,c("marital","balance")], marital == "married")
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Bijan
  • 7,737
  • 18
  • 89
  • 149
  • 7
    This is quite easy in `ggplot2`, but without a reproducible example it is hard to provide specific advice or code, see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for details. – Paul Hiemstra Nov 11 '13 at 09:49

1 Answers1

1

You could be more specific about your question. How exactly would you like to display your data? Anyway, here are a few possibilities. Hopefully you can use one of them for your purposes.

# load packages
pkgs2load <- c("data.table", "ggplot2", "data.table")
sapply(pkgs2load, require, character.only=TRUE)
# generate sample data
N <- 1e4
dt <- data.table(balance = runif(N, 0, 1e6),
                 status = sample(c("married", "unmarried", "divorced"), N, replace=TRUE))
# plots
ggplot(dt, aes(status, balance)) + stat_summary(fun.data = "mean_cl_boot")
ggplot(dt, aes(status, balance)) + geom_jitter()
ggplot(dt, aes(factor(0), balance, color=status)) + geom_jitter() + 
  scale_x_discrete(name="", breaks=NULL)
shadow
  • 21,823
  • 4
  • 63
  • 77