我正在尝试根据以下数据制作情节
dt <- data.frame(ValuationDate = seq.Date(as.Date("2014-1-1"),
as.Date("2014-7-1"), by = "month"),
Adjuster = factor(c("Bob","Bob","Sue","Sue","Sue","Sue","Bob")),
Paid = c(1,2,3,4,5,6,7), Incurred = c(3,5,9,9,10,8,7))
dt <- melt(dt, measure.vars = c("Paid", "Incurred"),
variable.name = "Value", value.name = "Amount")
dt
ValuationDate Adjuster Value Amount
1 2014-01-01 Bob Paid 1
2 2014-02-01 Bob Paid 2
3 2014-03-01 Sue Paid 3
4 2014-04-01 Sue Paid 4
5 2014-05-01 Sue Paid 5
6 2014-06-01 Sue Paid 6
7 2014-07-01 Bob Paid 7
8 2014-01-01 Bob Incurred 3
9 2014-02-01 Bob Incurred 5
10 2014-03-01 Sue Incurred 9
11 2014-04-01 Sue Incurred 9
12 2014-05-01 Sue Incurred 10
13 2014-06-01 Sue Incurred 8
14 2014-07-01 Bob Incurred 7
对于每个ValuationDate,有一个“付费”金额和一个“发生”金额我想在一段时间内绘制,将每个时间段与一个调整器相关联。当我尝试做
时#with grouping variable and linetype (error)
ggplot(data = dt) + geom_step(aes(x = ValuationDate,
y = Amount, group = Value,
color = Adjuster, linetype = Value))
我得到“错误:geom_path:如果你使用虚线或虚线,颜色,大小和线型必须在线上保持不变”
然而,以下两个图表工作
#with grouping variable, without linetype
ggplot(data = dt) + geom_step(aes(x = ValuationDate, y = Amount,
group = Value, color = Adjuster))
#with linetype, without grouping variable
ggplot(data = dt) + geom_step(aes(x = ValuationDate, y = Amount,
color = Adjuster, linetype = Value))
是什么给出了?
答案 0 :(得分:1)
让我们看一下产生错误的piece of code:
# Work out whether we should use lines or segments
attr <- ddply(munched, .(group), function(df) {
data.frame(
solid = identical(unique(df$linetype), 1),
constant = nrow(unique(df[, c("alpha", "colour","size", "linetype")])) == 1
)
})
solid_lines <- all(attr$solid)
constant <- all(attr$constant)
if (!solid_lines && !constant) {
stop("geom_path: If you are using dotted or dashed lines",
", colour, size and linetype must be constant over the line",
call.=FALSE)
}
因此,如果至少有两个不同的linetype
s,则所有其他aes应该是唯一的。我不能说为什么会出现这种限制,可能会导致一些与渲染相关的问题。
您可以通过其他方式区分线条,例如改变size
或alpha
而不是linetype
。另一种可能性是添加
geom_point(aes(x = ValuationDate, y = Amount, group = Value, shape = Value), size = 3)
答案 1 :(得分:1)
您必须为每个唯一的值x调整器段使用单独的geom_step调用。
gg <- ggplot()
for (i in 1:6) gg <- gg +
geom_step(data=subset(your_dt, Segment==i),
aes(x=ValuationDate, y=Amount, color=Adjuster, linetype=Value, group=i))
制作(致电gg
):
要使其连接到连续的阶梯线(即您想要的输出),您必须插入一些虚拟/重复行。我是手动完成的,但根据您执行此操作的频率,您可能需要一个函数来插入这些行。通过将Adjuster和Value与前一行进行比较,可以轻松计算Segment
。
your_dt:
ValuationDate Adjuster Value Amount DummyRow Segment
1 1/1/2014 Bob Paid 1 no 1
2 2/1/2014 Bob Paid 2 no 1
3 3/1/2014 Bob Paid 2 yes 1
4 3/1/2014 Sue Paid 2 yes 2
5 3/1/2014 Sue Paid 3 no 2
6 4/1/2014 Sue Paid 4 no 2
7 5/1/2014 Sue Paid 5 no 2
8 6/1/2014 Sue Paid 6 no 2
9 7/1/2014 Sue Paid 6 yes 2
10 7/1/2014 Bob Paid 6 yes 3
11 7/1/2014 Bob Paid 7 no 3
12 1/1/2014 Bob Incurred 3 no 4
13 2/1/2014 Bob Incurred 5 no 4
14 3/1/2014 Bob Incurred 5 yes 4
15 3/1/2014 Sue Incurred 5 yes 5
16 3/1/2014 Sue Incurred 9 no 5
17 4/1/2014 Sue Incurred 9 no 5
18 5/1/2014 Sue Incurred 10 no 5
19 6/1/2014 Sue Incurred 8 no 5
20 7/1/2014 Sue Incurred 8 yes 5
21 7/1/2014 Bob Incurred 8 yes 6
22 7/1/2014 Bob Incurred 7 no 6