【问题标题】:How to plot expected values and the reference value in R?如何在 R 中绘制期望值和参考值?
【发布时间】:2015-06-21 12:21:18
【问题描述】:

我用点和误差线绘制每个位置的观察值,这符合预期。

问题:

  1. 我还尝试将每个位置的 预期 值绘制为一条短水平线,以与点相同的方式居中。我为此使用了线段,线条出现了,但是它们没有居中,我不知道如何控制它们。

  2. 我想绘制 reference 值,该值在所有地方每年都相同,但每年都在变化。我得到了 geom_line 的线,但它是倾斜的,我想要有步骤(或有中断的两条线)。使用 geom_steps 我得到每个地方的垂直移动,而不是每年。我也尝试了geom_hline,但是用hline.data<-data.frame(hy=chunk$Reference.Value, hx=chunk$Year) 定义了一个data.frame,沿着整个情节得到两条平行线。

你能告诉我我做错了什么吗? 非常感谢您!

我尝试的代码:

library(data.table)
library(ggplot2)

chunk<-fread("
Year;Places;Code;Name;Reference.Value;Value;Expected.Value;CI
2010;Place5;A12;Indicator;0.079;0.087;0.082;0.00286
2010;Place1;A12;Indicator;0.079;0.075;0.086;0.02317
2010;Place2;A12;Indicator;0.079;0.059;0.069;0.01955
2010;Place3;A12;Indicator;0.079;0.065;0.067;0.02712
2010;Place4;A12;Indicator;0.079;0.091;0.101;0.03211
2011;Place5;A12;Indicator;0.077;0.062;0.058;0.00260
2011;Place1;A12;Indicator;0.077;0.078;0.069;0.01736
2011;Place2;A12;Indicator;0.077;0.029;0.037;0.02821
2011;Place3;A12;Indicator;0.077;0.062;0.059;0.02166
2011;Place4;A12;Indicator;0.077;0.110;0.099;0.06452")

chunk[,`:=` (Year     = as.factor(Year),
               Places   = as.factor(Places),
               Code     = as.factor(Code),
               Name     = as.factor(Name)
)]

limits <- aes(ymax = Value+CI, ymin=Value-CI)
dodge <- position_dodge(width=0.9)

p <- ggplot(chunk, aes(colour=Places, fill=Places, y=Value, x=Year))

p + geom_linerange(limits, position=dodge) +
geom_point(position=dodge, size = 5) +
geom_segment(position=dodge,
           aes(group=Places, 
               y=Expected.Value, yend=Expected.Value, 
               x=as.numeric(Year)-0.01, xend=as.numeric(Year)+0.01)
) +
geom_line(position=dodge, aes(group=Places, y=Reference.Value,x=Year),   color="black")
# geom_step(position=dodge, direction="vh", aes(group=Places,y=Reference.Value,x=Year), color="black")

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    这是我的解释。您可以获得“闪避”偏移并将其用于线段的开始/结束。所有几何图形中的group 将是Year,并且由闪避确定的干扰只是说明Place 因素应该离该分组多远。

    ## Get offsets for dodge
    dodgeWidth <- 0.9
    nFactors <- length(levels(chunk[,Places]))
    chunk[, `:=` (dPlaces = (dodgeWidth/nFactors)*(as.numeric(Places) - mean(as.numeric(Places))))]
    
    limits <- aes(ymax = Value+CI, ymin=Value-CI)
    dodge <- position_dodge(width=0.9)
    p <- ggplot(chunk, aes(colour=Places, fill=Places, y=Value, x=Year))
    
    p + geom_linerange(limits, position=dodge) +
      geom_point(position=dodge, size = 5) +
      geom_segment(position=dodge, aes(y=Expected.Value, yend=Expected.Value, 
                       x=as.numeric(Year)+dPlaces-0.1, xend=as.numeric(Year)+dPlaces+0.1)) +
      geom_step(aes(group=Year, y=Reference.Value, 
                    x=as.numeric(Year)+dPlaces), color="black", linetype=2)
    

    【讨论】:

    • 这很有帮助!非常感谢!
    【解决方案2】:

    您可以将geom_errorbarstat = "hline" 一起使用,就像在this answer 中一样,让水平线按您想要的方式躲避。您将使用两次geom_errorbar,一次用于添加Expected.Value 行,一次用于添加Reference.Value 行。

    ggplot(chunk, aes(colour=Places, fill=Places, y=Value, x=Year)) +
        geom_linerange(limits, position=dodge) +
        geom_point(position=dodge, size = 5) +
        geom_errorbar(stat = "hline", aes(yintercept = Expected.Value, ymax = ..y.., ymin = ..y..), 
                    position = dodge, width = .8)  +
        geom_errorbar(stat = "hline", aes(group = Year, yintercept = Reference.Value, ymax = ..y.., ymin = ..y..), 
                    position = dodge, color="black", width = .8, linetype = 2)
    

    编辑

    stat = "hline" 方法在 R 的开发版本中不再适用于这个github issue。但是,可以直接使用geom_errorbar 来达到相同的效果。

    ggplot(chunk, aes(colour=Places, fill=Places, y=Value, x=Year)) +
        geom_linerange(limits, position=dodge) +
        geom_point(position=dodge, size = 5) +
        geom_errorbar(aes(y = Expected.Value, ymax = ..y.., ymin = ..y..), 
                    position = dodge, width = .8)  +
        geom_errorbar(aes(group = Year, y = Reference.Value, ymax = ..y.., ymin = ..y..), 
                    position = dodge, color="black", width = .8, linetype = 2)
    

    【讨论】:

    • 这是一个非常有趣的选项,非常感谢!
    • 如果我敢的话,还有一个问题 :-) 我理解 ..y.. 的含义,但找不到相关文档。你能指点我吗?非常感谢!
    • @v_e 这些特殊变量最常出现在相应stat_* 函数的文档中(有关一些信息,请参阅herehere)。在这种情况下,我实际上还没有找到文档,但..y.. 是避免在使用geom_errorbar 时重写yintercept 变量的名称的捷径,因为ymaxymin 是必需的美学在geom_errorbar 中,但与水平线无关。
    • 再次感谢!没有你我找不到这个。
    猜你喜欢
    • 1970-01-01
    • 2019-06-27
    • 2017-09-12
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 2019-01-27
    相关资源
    最近更新 更多