2017-04-16 60 views
1

我想有误差条线图。线图和误差条

我有数据如下:

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) 

脚本绘制折线图和误差线如下:

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 

的问题是,该错误栏不会出现。

任何帮助?

回答

0

你的代码有,我注意到并修复了一些错误。如果你改变你的geom_errorbar调用下面应该修复错误吧:

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

而且,在你的代码的第一行更改data$servers是一个因素。你不能有这是一个因素,并呼吁scale_x_continuous - 你必须要么设置data$servers是数字或使用scale_x_discrete。我只是选择保留data$servers作为数字。

此代码应工作:

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 

enter image description here

编辑: 你errorbars似乎是水平的原因是因为垂直值是如此之小。您错误栏的y值是从data$Throughput加上/减去data$Error。当data$Throughput是2000,你是加/减3.5(这是什么data$Error是)你不会看到竖线。

你或许应该重新考虑你的Error列的规模是什么。这里是同积300乘以data$Error

enter image description here

+0

试好。但是,如何使错误栏垂直?原因错误条与y轴相关而不是x轴@Mike –

+0

它们是垂直的......您只是加/减一小部分实际吞吐量值。尝试乘以你的'数据$错误'100. –

+0

它的工作原理,你能解释为什么我们应该多100错误? @Mike H –