在R中绘制线段
我想在R中绘制分段数据,也就是说,我有表格中的数据在R中绘制线段,r,plot,segment,R,Plot,Segment,我想在R中绘制分段数据,也就是说,我有表格中的数据 | Product | Date | Origination | Rate | Num | Balance | |-----------------------|--------|-------------|------|-----|-----------| | DEMAND DEPOSITS | 200505 | 198209 | 0 | 1 | 2586.25 | |
| 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格式,因此如果需要更改回该格式,请告诉我
我还没能在网上找到一个信息丰富的解决方案,尽管我肯定有
谢谢,如@zx8754所示,您应该提供可复制的数据。 在没有测试代码(因为没有可复制的数据)的情况下,假设数据在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的答案,第一个绘图可以是绘图(Balance~Origination,data=data,type='n')
,然后在i_产品的1:length(所有产品)
中完成后续的行。这样,您就不必担心ylim
。下面是一个使用格伦菲尔德数据的示例
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)
z我想你想要的东西如下:
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)
df我不知道你想要什么,这就是你想要的吗
假设您将数据放入data.txt,删除管道并将名称中的空格替换为“\ux”
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" )
请提供数据并展示一些代码,你尝试过什么但失败了。太棒了。这很有效,而且非常简单。最后,我不得不重新启动R以使其正常工作。谢谢。这是一个很好的解决方案,可以使用许多不同的产品。彩虹功能的使用非常简洁。谢谢,我很感激。这是一个非常巧妙的情节。不完全是我想要的。其目的是按起始日期构建产品余额的时间序列。但这种绘图技术在未来肯定会有用。谢谢。对于你接下来的问题,不要犹豫,用手画一幅你想要的草图,一幅画值千言万语;)