使用线型和组美学的ggplot错误 [英] ggplot error using linetype and group aesthetics

查看:45
本文介绍了使用线型和组美学的ggplot错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据以下数据进行绘图

I'm trying to make plot based on the following data

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,我想随时间绘制已支付"金额和已发生"金额,并将每个时间段与一个调节器相关联.当我尝试做

For each ValuationDate, there is a "Paid" amount and an "Incurred" amount which I'd like to plot over time, associating each time period with an Adjuster. When I try doing

#with grouping variable and linetype (error)
ggplot(data = dt) + geom_step(aes(x = ValuationDate, 
                                  y = Amount, group = Value, 
                                  color = Adjuster, linetype = Value))

我收到错误:geom_path:如果使用虚线或虚线,则颜色,大小和线型必须在该线上保持不变"

I get "Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line"

但是以下两个情节有效

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

有什么作用?

推荐答案

您将必须对每个唯一的Value x Adjuster段使用单独的geom_step调用.

You will have to use separate geom_step calls for each unique Value x Adjuster segment.

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 .

To rig it to get continuous stepped lines (i.e. the output you want), you have to insert some dummy/duplicate rows. I did that manually, but depending on how often you are doing this you'll probably want a function to insert these rows. Segment can be easily calculated by comparing Adjuster and Value to the prior row.

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

这篇关于使用线型和组美学的ggplot错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆