2016-10-04 86 views
1

我想在R.即绘制分段的数据,说我有形式图形细分

| 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 | 

我想通过在Y轴绘制每“产品”类型的线的数据'平衡'。在X轴上,我想放置'起源'。我理想的情况下也喜欢设置颜色来区分线条。该数据目前不在data.frame窗体中,所以让我知道是否需要改回这一点。

我一直没有能够在网上找到这个信息的解决方案,即使我确定有。

感谢,

+0

更换空间的名称请数据[重复性(HTTP:/ /stackoverflow.com/questions/5963269),并显示一些代码的努力,你有什么尝试和失败。 – zx8754

回答

0

我猜你想要的东西,像下面这样:

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

+0

太棒了。这工作很简单。我最终不得不重新启动R才能正常工作。谢谢。 – user5619709

1

正如@ zx8754 menitioned,你应该提供可重复的数据。 无需测试的代码(因为没有重复性的数据),我建议如下,假设数据是在data.frame“数据”:

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]) 
} 
+0

这是使用许多不同产品的很好的解决方案。彩虹功能的使用很整齐。谢谢,我很感激。 – user5619709

1

我没有足够的信誉发表评论,所以我把它写成答案。为了让@ tobiasegli_te的回答更短,第一个plot可以是plot(Balance~Origination,data=data,type='n'),然后为i_product in 1:length(all_products)完成后续的lines。这样你就不用担心ylim。这里是一个使用Grunfeld数据的例子。

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) 

另请注意,您的Origination并非等距。您需要将其更改为Date或类似的。

0

你我想我不知道,是你要寻找什么?

ex_answer

假设你把你的数据在data.txt中,去除管道和“_”

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") 
+0

这是一个非常整洁的情节。不是我正在寻找的东西。它是按发货日期构建产品余额的时间序列。但是这种绘图技术在将来肯定会有用。谢谢。 – user5619709

+0

对于你的下一个问题,不要犹豫,手绘一幅你想要的素描,一张价值千言万语的图片;) –