如何使用边界绘制数据

时间:2012-07-22 10:59:18

标签: r data-visualization

我有这样的数据:

    yr      X  lower  upper
1 2004 0.2852 0.3927 0.1888
2 2005 0.3710 0.2385 0.5093
3 2006 0.3297 0.2177 0.4557
4 2007 0.2230 0.1424 0.3138
5 2008 0.3028 0.1952 0.4237
6 2009 0.3906 0.2798 0.5226
7 2010 0.3382 0.2343 0.4467

以下是一些可重现的数据:

dt <- structure(list(yr = 2004:2010, X = c(0.2852, 0.371, 0.3297, 0.223, 0.3028, 0.3906, 0.3382), lower = c(0.3927, 0.2385, 0.2177, 0.1424, 0.1952, 0.2798, 0.2343), upper = c(0.1888, 0.5093, 0.4557, 0.3138, 0.4237, 0.5226, 0.4467)), .Names = c("yr", "X", "lower", "upper"), class = "data.frame", row.names = c(NA, -7L))

我想绘制这个,结果将在演示中进行,所以我想让它看起来尽可能好 - 我很抱歉使用主观的“好”但我不知道如何否则说出来!我试过这个:

library(ggplot2)

ggplot(dt, aes(x=yr, y=X, group=1)) +
    geom_line() +
    geom_errorbar(width=.1, aes(ymin=lower, ymax=upper)) +
    geom_point(shape=21, size=3, fill="blue") +
    ylim(0,0.6)

但我不喜欢结果 - 它似乎显而易见且无聊: enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用功能区而不是错误栏

dt <- structure(list(yr = 2004:2010, 
    X = c(0.2852, 0.371, 0.3297, 0.223, 0.3028, 0.3906, 0.3382), 
    lower = c(0.3927, 0.2385, 0.2177, 0.1424, 0.1952, 0.2798, 0.2343), 
    upper = c(0.1888, 0.5093, 0.4557, 0.3138, 0.4237, 0.5226, 0.4467)),
  .Names = c("yr", "X", "lower", "upper"), class = "data.frame", 
    row.names = c(NA, -7L))
library(ggplot2)
ggplot(dt, aes(x=yr, y=X, group=1, ymin = lower, ymax = upper)) +
    geom_ribbon(alpha = 0.2) +
    geom_line() +
    geom_point(shape=21, size=3, fill="blue") +
    ylim(0,0.6)

enter image description here