简体   繁体   English

线图和错误栏

[英]Line graph and error bar

I would like to have line graph with error bars. 我想要带有误差线的折线图。

I have data as following: 我有以下数据:

data <- read.table(text = "servers  Throughput  Error   Protocols
3   3400    3.45    Z1
5   2300    3.45    Z1
7   1700    3.45    Z1
9   1290    3.45    Z1
3   5000    1.064564    Z2
5   2500    1.064564    Z2
7   1800    1.064564    Z2
9   1400    1.064564    Z2
3   4500    1.064564    Z3
5   2490    1.064564    Z3
7   1780    1.064564    Z3
9   1370    1.064564    Z3", header = TRUE)

The script to draw the line graphs and error bars as follows: 绘制折线图和误差线的脚本如下:

data$servers <- as.factor(data$servers)

plot1 <- ggplot(data=data , aes(x=servers, y=Throughput, group=Protocols, colour = Protocols, shape=Protocols)) +
    geom_errorbar(aes(plot1,ymin=Throughput-Error, ymax=Throughput+Error) +
    geom_line() +
    geom_point(size=3)

plot1 <- plot1 + scale_y_continuous(breaks= seq(0,5500,500), limits = c(0,5500)) + 
    labs(x="size") +
    scale_x_continuous(breaks = c(3, 5, 7,9)) + 
    labs(y="Throughput (ops/sec)")

plot1 <- plot1 + scale_colour_manual(values=c("#66CC99","#997300", "#6c3483"))

plot1 <- plot1 + theme_bw() + 
    theme(legend.position="bottom") + 
    labs(fill="", colour=" ", shape=" ") + 
    theme(text = element_text(size=18)) + 
    guides(fill = guide_legend(keywidth = 0.8, keyheight = 0.01))

plot1

The problem is that The error bar does not appear. 问题是错误栏没有出现。

Any help? 有帮助吗?

Your code had a few errors that I noticed and fixed. 您的代码有一些我注意到并修复的错误。 If you change your geom_errorbar call to the below that should fix the error bars: 如果您将geom_errorbar调用更改为以下应修复错误栏:

geom_errorbar(aes(ymin=Throughput-Error, ymax=Throughput+Error))

Also, in the first line of your code you change data$servers to be a factor. 此外,在代码的第一行中,您将data$servers更改为一个因素。 You can't have this be a factor and call scale_x_continuous - you have to either set data$servers to be numeric or you use scale_x_discrete . 您不能将此作为一个因素并调用scale_x_continuous - 您必须将data$servers设置为数字或使用scale_x_discrete I just chose to keep data$servers as numeric. 我只是选择将data$servers为数字。

This code should work: 这段代码应该有效:

plot1 <- ggplot(data=data , aes(x=servers, y=Throughput, group=Protocols, colour = Protocols, shape=Protocols)) +
                geom_errorbar(aes(ymin=Throughput-Error, ymax=Throughput+Error)) +
                geom_line() +
                geom_point(size=3)

          plot1 <- plot1 + scale_y_continuous(breaks= seq(0,5500,500), limits = c(0,5500)) + 
            labs(x="size") +
            scale_x_continuous(breaks = c(3, 5, 7,9)) + 
            labs(y="Throughput (ops/sec)")

          plot1 <- plot1 + scale_colour_manual(values=c("#66CC99","#997300", "#6c3483"))

          plot1 <- plot1 + theme_bw() + 
            theme(legend.position="bottom") + 
            labs(fill="", colour=" ", shape=" ") + 
            theme(text = element_text(size=18)) + 
            guides(fill = guide_legend(keywidth = 0.8, keyheight = 0.01))

          plot1

在此输入图像描述

Edit: The reason your errorbars appear to be horizontal is because the vertical values are so small. 编辑:您的错误栏看起来是水平的原因是因为垂直值太小。 Your y values for the error bar are adding/subtracting data$Error from data$Throughput . 您的错误栏的y值是从data$Throughput中添加/减去data$Error When data$Throughput is 2000 and you are adding/subtracting 3.5 (which is about what data$Error is) you won't see the vertical bars. data$Throughput为2000并且您正在添加/减去3.5(这是关于data$Error )时,您将看不到垂直条。

You should probably rethink what the scale for your Error column is. 您可能应该重新考虑Error列的比例。 Here is the same plot multiplying data$Error by 300: 以下是将data$Error乘以300的相同图:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM