1

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,

user5619709
  • 53
  • 2
  • 11
  • Please make the data [reproducible](http://stackoverflow.com/questions/5963269) and show some code effort, what have you tried and failed. – zx8754 Oct 04 '16 at 15:33

4 Answers4

1

As @zx8754 menitioned, you should provide reproducible data. Without having tested the code (because there's no reproducible data), I would suggest the following, assuming that the data is in the data.frame 'data':

all_products <- unique(data$Product)
colors_use <- rainbow(length(all_products))

plot(y = data[data$Product == all_products[1],"Balance"],
    x = data[data$Product == all_products[1],"Origination"],
    type = "l",
    col = colors_use[1],
    ylim = c(min(data$Balance, na.rm = T),max(data$Balance, na.rm = T)),
    xlim = c(min(data$Origination, na.rm = T),max(data$Origination, na.rm = T)))

for(i_product in 2:length(all_products)){
    lines(y = data[data$Product == all_products[i_product],"Balance"],
        x = data[data$Product == all_products[i_product],"Origination"],
        col = colors_use[i_product])
}
tobiasegli_te
  • 1,413
  • 1
  • 12
  • 18
  • This is a nice solution for using many different products. The use of the rainbow functionality was neat. Thanks, I appreciate it. – user5619709 Oct 04 '16 at 17:13
1

I have not enough reputation to comment, so I write it as an answer. To make @tobiasegli_te's answer shorter, the first plot can be plot(Balance~Origination,data=data,type='n') and then make the subsequent lines done for i_product in 1:length(all_products). That way you need not worry about ylim. Here is an example using the Grunfeld data.

z <- read.csv('http://statmath.wu-wien.ac.at/~zeileis/grunfeld/Grunfeld.csv')
plot(invest~year,data=z,type='n')
for (i in unique(as.numeric(z$firm))) lines(invest~year,data=z,
    subset=as.numeric(z$firm)==i, col=i)

Also note that your Origination is not equally spaced. You need to change it to a Date or similar.

chan1142
  • 609
  • 4
  • 13
0

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

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
  • Awesome. This worked and was pretty simple. I ended up having to restart R to get it to work correctly. Thanks. – user5619709 Oct 04 '16 at 17:13
0

I am not sure what you my want, is that what you re looking for?

ex_answer

Assuming you put your data in data.txt, removing the pipes and replacing the spaces in the names by '_'

d = read.table("data.txt", header=T)

prod.col = c("red", "blue", "green", "black" )
prod = unique(d$Product)

par(mai = c(0.8, 1.8, 0.8, 0.8))
plot(1, yaxt = 'n', type = "n", axes = TRUE, xlab = "Origination", ylab = "", xlim = c(min(d$Origination), max(d$Origination)), ylim=c(0, nrow(d)+5) )
axis(2, at=seq(1:nrow(d)), labels=d$Product, las = 2, cex.axis=0.5)
mtext(side=2, line=7, "Products")

for( i in 1:nrow(d) ){
myProd = d$Product[i]
myCol = prod.col[which(prod == myProd)]
myOrig  = d$Origination[i]
segments( x0 = 0, x1 = myOrig, y0 = i, y1 = i, col = myCol, lwd = 5 )
}
legend( "topright", col=prod.col, legend=prod, cex=0.3, lty=c(1,1), bg="white" )
Xavier Prudent
  • 1,570
  • 3
  • 25
  • 54
  • That's a really neat plot. Not quite what I was looking for. It was to construct a time series of the product balances by origination date. But that plotting technique might be useful in the future for sure. Thanks. – user5619709 Oct 04 '16 at 18:19
  • For your next questions, don't hesitate to draw by hand a sketch of what you want, a picture worths thousand words ;) – Xavier Prudent Oct 05 '16 at 15:19