[英]Line graph and error bar
我想要帶有誤差線的折線圖。
我有以下數據:
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
問題是錯誤欄沒有出現。
有幫助嗎?
您的代碼有一些我注意到並修復的錯誤。 如果您將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
編輯:您的錯誤欄看起來是水平的原因是因為垂直值太小。 您的錯誤欄的y值是從data$Throughput
中添加/減去data$Error
。 當data$Throughput
為2000並且您正在添加/減去3.5(這是關於data$Error
)時,您將看不到垂直條。
您可能應該重新考慮Error
列的比例。 以下是將data$Error
乘以300的相同圖:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.