2017-04-16 61 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 –