【问题标题】:ggplot error using linetype and group aesthetics使用线型和组美学的ggplot错误
【发布时间】:2014-11-18 22:24:29
【问题描述】:

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

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

什么给了?

【问题讨论】:

  • 您在寻找什么输出?总共有多少行,代表什么?
  • 我的输出应该有两行,与我显示的第一张图非常相似。不过,该图表的问题在于,什么是“付费”和什么是“发生”之间没有区别。

标签: r ggplot2


【解决方案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)
}

因此,如果至少有两个不同的linetypes,则所有其他 aes 都应该是唯一的。我不能说为什么会出现这个限制,可能它导致了一些渲染相关的问题。

您可以通过其他方式区分线条,例如通过改变sizealpha 而不是linetype。另一种可能性是添加

geom_point(aes(x = ValuationDate, y = Amount, group = Value, shape = Value), size = 3)

【讨论】:

    【解决方案2】:

    您必须为每个唯一的 Value x Adjuster 段使用单独的 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):

    为了获得连续的阶梯线(即您想要的输出),您必须插入一些虚拟/重复行。我是手动完成的,但根据您执行此操作的频率,您可能需要一个函数来插入这些行。 Segment 可以通过将 Adjuster 和 Value 与前一行进行比较来轻松计算。

    你的_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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多