按特定顺序排列图例

时间:2017-09-08 10:30:25

标签: r ggplot2

我想在折线图中订购图例。 我有以下数据:

km <- read.table(text = "
    Write Ratio Latency Protocols   Error   order
10  39.48518787 Z   1.690797345 6
20  42.57837188 Z   2.09389683  6
30  43.81207003 Z   2.100299843 6
40  44.64019337 Z   1.731677411 6
50  43.56321452 Z   1.599011513 6
10  30.94171175 ZAc 1.465293005 5
20  33.55340099 ZAc 1.857446286 5
30  33.28043526 ZAc 1.717274835 5
40  35.10099032 ZAc 1.64346335  5
50  35.28632906 ZAc 2.000844669 5
10  33.03570067 ZAa 1.540959195 4
20  38.66104687 ZAa 1.853149998 4
30  37.38151025 ZAa 1.939120563 4
40  38.75633582 ZAa 1.659655061 4
50  39.33163533 ZAa 1.441354301 4
10  30.21479655 ZCt 1.428351052 3
20  36.89743631 ZCt 1.238552328 3
30  36.84339334 ZCt 2.325601015 3
40  37.12947518 ZCt 2.348771154 3
50  36.58073178 ZCt 1.164013579 3
10  35.96830252 ZAA 1.609300035 2
20  41.94394296 ZAA 1.932646116 2
30  41.58329277 ZAA 1.853433741 2
40  42.72808214 ZAA 1.545654993 2
50  42.57124314 ZAA 1.569868536 2
10  34.79607463 ZCT 1.242303881 1
20  40.81393655 ZCT 1.332433183 1
30  39.35399878 ZCT 1.931448023 1
40  38.94434492 ZCT 2.089489871 1
50  40.97601259 ZCT 1.417015937 1", header = TRUE)

使用其图例绘制折线图的脚本如下:

km$Write.Ratio <- as.numeric(km$Write.Ratio)
km <- transform(km, Protocols= reorder(Protocols, order(order, decreasing = T)))

plot1 <- ggplot(km, aes(Write.Ratio, Latency, fill = Protocols))
plot1 <- ggplot(data=km , aes(x=Write.Ratio, y=Latency, group=Protocols, colour = Protocols, shape=Protocols)) +
geom_errorbar(aes(ymin=Latency-Error, ymax=Latency+Error), width=2.0, colour="black") +
geom_line(size=1.2) +
geom_point(size=3.2)
plot1 <- plot1 + scale_y_continuous(breaks= seq(0,60,10), limits = c(0,60)) + 
labs(x="Ratio") +
scale_x_continuous(breaks = c(10,20,30,40,50)) + 
labs(y="Latency (ms)") 
plot1 <- plot1 + scale_colour_manual(values=c("#00ff00","#ff0000","#DAA520","#00bfff","#CD853F","#0000ff"))
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

我想传说的顺序如下: Z ZAc ZAa ZCt ZAA ZCT。

下图有错误的传说顺序,这是我目前的尝试。 This is my try but it does not order the legends as I need

任何帮助?

0 个答案:

没有答案