2015-06-21 140 views
3

我使用点和误差线绘制每个地点的观察值,该值与预期一致。如何绘制R中的期望值和参考值?

  1. 我也尝试绘制每个地方预期值作为短横线,以同样的方式为分中心:

    问题。我使用细分为这个,线条出现,但他们不居中,我不知道如何控制它们。

  2. 我想绘制的参考价值是每年相同的所有地方,但每年的变化相同。我用geom_line得到这一行,但它是斜的,我想要有步骤(或者带有中断的两行)。通过geom_steps,我可以获得每个位置的垂直位移,而不是每年。我尝试了geom_hline,但是通过定义一个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") 

回答

2

这里是我的解释。您可以获得“闪避”偏移量,并将这些偏移量用于线段的起点/终点。在所有几何中的group将是Year,并且由闪避确定的消息只是说距离该组应该是多远。

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

enter image description here

+0

这非常有帮助!非常感谢! –

1

您可以使用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) 

enter image description here

编辑

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

这是一个非常有趣的选项,非常感谢! –

+0

另一个问题,如果我敢:-)我明白什么'..y..'的意思,但无法找到这个文件。你能指点我吗?非常感谢! –

+1

@v_e这些特殊变量通常在适当的'stat_ *'函数的文档中(参见[这里](http://stackoverflow.com/questions/15556069/documentation-for-special-variables-in-ggplot-计数密度等)和[这里](https://groups.google.com/forum/#!topic/ggplot2/I9PVlFAAe9U)的一些信息)。在这种情况下,我实际上没有找到文档,但'..y ..'是避免重写'yintercept'变量名称的捷径,因为'ymax'和'ymin'在'geom_errorbar'中需要美观,但与水平线无关,因此使用'geom_errorbar'时会这样。 – aosmith