Search code examples
rplotsegment

Graphing Segments in R


I want to plot segmented data in R. That is, say I have data of the form

| Product               | Date   | Origination | Rate | Num | Balance   |
|-----------------------|--------|-------------|------|-----|-----------|
| DEMAND DEPOSITS       | 200505 | 198209      | 0    | 1   | 2586.25   |
| DEMAND DEPOSITS       | 200505 | 198304      | 0    | 1   | 3557.73   |
| DEMAND DEPOSITS       | 200505 | 198308      | 0    | 1   | 14923.72  |
| DEMAND DEPOSITS       | 200505 | 198401      | 0    | 1   | 4431.67   |
| DEMAND DEPOSITS       | 200505 | 198410      | 0    | 1   | 44555.23  |
| MONEY MARKET ACCOUNTS | 200505 | 198209      | 0.25 | 2   | 65710.01  |
| MONEY MARKET ACCOUNTS | 200505 | 198211      | 0.25 | 2   | 41218.41  |
| MONEY MARKET ACCOUNTS | 200505 | 198304      | 0.25 | 1   | 61421.2   |
| MONEY MARKET ACCOUNTS | 200505 | 198402      | 0.25 | 1   | 13620.17  |
| MONEY MARKET ACCOUNTS | 200505 | 198408      | 0.75 | 1   | 281897.74 |
| MONEY MARKET ACCOUNTS | 200505 | 198410      | 0.25 | 1   | 5131.33   |
| NOW ACCOUNTS          | 200505 | 198209      | 0    | 1   | 142744.35 |
| NOW ACCOUNTS          | 200505 | 198303      | 0    | 1   | 12191.6   |
| SAVING ACCOUNTS       | 200505 | 198301      | 0.25 | 1   | 96936.24  |
| SAVING ACCOUNTS       | 200505 | 198302      | 0.25 | 2   | 21764     |
| SAVING ACCOUNTS       | 200505 | 198304      | 0.25 | 1   | 14646.55  |
| SAVING ACCOUNTS       | 200505 | 198305      | 0.25 | 1   | 20909.7   |
| SAVING ACCOUNTS       | 200505 | 198306      | 0.25 | 1   | 66434.56  |
| SAVING ACCOUNTS       | 200505 | 198309      | 0.25 | 1   | 20005.56  |
| SAVING ACCOUNTS       | 200505 | 198404      | 0.25 | 2   | 16766.56  |
| SAVING ACCOUNTS       | 200505 | 198407      | 0.25 | 1   | 47721.97  |

I want to plot on the Y-axis a line per 'Product' type by 'Balance'. On the X-axis, I want to put the 'Origination'. I would ideally also like to set colors to distinguish between the lines. The data is not currently in data.frame form so let me know if I need to change back to that.

I haven't been able to find an informative solution online for this, even though I'm sure there is.

Thanks,


Solution

  • I guess you want something like the following:

    df <- as.data.frame(df[c('Product', 'Balance', 'Origination')])
    head(df)
    
    Product  Balance Origination
    1  DEMAND DEPOSITS         2586.25      198209
    2  DEMAND DEPOSITS         3557.73      198304
    3  DEMAND DEPOSITS        14923.72      198308
    4  DEMAND DEPOSITS         4431.67      198401
    5  DEMAND DEPOSITS        44555.23      198410
    6  MONEY MARKET ACCOUNTS  65710.01      198209
    
    library(ggplot2)
    library(scales)
    ggplot(df, aes(Origination, Balance, group=Product, col=Product)) + 
             geom_line(lwd=1.2) + scale_y_continuous(labels = comma) 
    

    enter image description here