Errorbars看起来像pointrange(ggplot2)

时间:2013-11-14 03:45:15

标签: r ggplot2 statistics

我有以下数据框:

> df <- read.table("throughputOverallSummary.txt", header = TRUE)
> df
  ExperimentID clients connections msgSize      Mean Deviation     Error
1           77     100          50    1999 142.56427  8.368127 0.4710121
2           78     200          50    1999 284.22705 13.575943 0.3832827
3           79     400          50    1999 477.48997 44.820831 0.7538666
4           80     600          50    1999 486.87102 49.916391 0.8240869
5           81     800          50    1999 488.84899 51.422070 0.8462216
6           82      10          50    1999  15.23667  1.995150 1.0498722
7           83      50          50    1999  71.94000  5.197893 0.5793057

以及处理上述数据框df的一些代码:

msg_1999 = subset(df, df$msgSize == 1999)
if (nrow(msg_1999) > 0) {
    limits = aes(ymax = msg_1999$Mean + msg_1999$Deviation, ymin = msg_1999$Mean -   
    msg_1999$Deviation)
    ggplot(data = msg_1999, aes(clients, Mean, color = as.factor(connections), group =     
       as.factor(connections))) +
       geom_point() + geom_line() +
       geom_errorbar(limits, width = 0.25) +
       xlab("Number of Clients") +
       ylab("Throughput (in messages/second)") +
       labs(title = "Message size 1999 bytes", color = "Connections") 

    ggsave(file = "throughputMessageSize1999.png")
}

我的问题是图中的误差条看起来像点范围。错误条的上端和下端的水平条缺失。

enter image description here

理想情况下,错误栏应该看起来像这样:

enter image description here

为什么我的代码中的错误栏看起来不同?

1 个答案:

答案 0 :(得分:2)

width参数与x的缩放比例相同,您已width = 0.25,其中x轴的范围为0-800。宽度为0.25的条形图在此图表中不可见。如果你没有设置宽度值,那么就会猜到一些合理合理的东西。

ggplot(data = df, aes(clients, Mean, color = as.factor(connections), group =     
                                as.factor(connections))) +
    geom_point() + geom_line() +
    geom_errorbar(aes(ymax = Mean + Deviation, ymin=Mean-Deviation)) +
    xlab("Number of Clients") +
    ylab("Throughput (in messages/second)") +
    labs(title = "Message size 1999 bytes", color = "Connections")

enter image description here

请注意,如果要预定义mapping参数,仍应像调用geom_xxxx一样指定变量。 aes(和ggplot)做了一些花哨的步法,以确保在绘图时在正确的环境中对其进行评估。

因此以下内容将起作用

limits <- aes(ymax = Mean + Deviation, ymin=Mean-Deviation)
ggplot(data = df, aes(clients, Mean, color = as.factor(connections), group =     
                                as.factor(connections))) +
    geom_point() + geom_line() +
    geom_errorbar(limits) +
    xlab("Number of Clients") +
    ylab("Throughput (in messages/second)") +
    labs(title = "Message size 1999 bytes", color = "Connections")