简体   繁体   English

使用ggplot2对具有分段的多线图重新排序图例

[英]reorder legend of multiple line plot with segment using ggplot2

I have a multiple line plot with a segment of each line highlighted the same way as discussed here . 我有一个多线图,每条线的一部分都以与此处讨论的相同的方式突出显示。 Here is the reproducible example: 这是可重现的示例:

df <- data.frame(x = 1:100,y1 = rnorm(100,1,100),y2=rnorm(100,5,50),y3=rnorm(100,10,500),y4=rnorm(100,1,200),col1 = c(rep("red", 50), rep("black", 10), rep("red", 40)),col2=c(rep("blue", 50), rep("black", 10), rep("blue", 40)),col3=c(rep("orange", 50), rep("black", 10), rep("orange", 40)),col4=c(rep("cyan", 50), rep("black", 10), rep("cyan", 40)))

ggplot(df, aes(x=x, y=y1)) + geom_line(aes(colour=col1, group=1))+geom_line(aes(x=x, y=y2,col=col2,group=1))+geom_line(aes(x=x, y=y3,col=col3,group=1))+geom_line(aes(x=x, y=y4,colour=col4, group=1))+geom_line(aes(x=x, y=y4,col=col4,group=1))+scale_color_manual(values=c("black","blue","red","orange","cyan"),labels=c("new","s1on","s2off","s2_on","s1_off"),name="")

This gives the order of the items in the legend as shown in the image below. 这给出了图例中项目的顺序,如下图所示。 I want to reorder them. 我想重新排序。 Since each line has a segment and each segment has the same color "black", I am not able to use breaks argument in scale_color_manual as it requires 5 values and the plot is plotted for 4 columns in the dataframe. 由于每一行都有一个段,每个段具有相同的颜色“黑色”,因此我无法在scale_color_manual使用breaks参数,因为它需要5个值,并且在数据scale_color_manual为4列绘制了该图。 How can I reorder the items in the legend in the order s1on, s2on, s1_off, s2_off and new? 如何按s1on,s2on,s1_off,s2_off和new的顺序对图例中的项目重新排序? 在此处输入图片说明

This might be what you are looking for 这可能是您要寻找的

 ggplot(df, aes(x=x, y=y1)) + 
  geom_line(aes(colour=col1, group=1)) +
  geom_line(aes(x=x, y=y2,col=col2,group=1)) +
  geom_line(aes(x=x, y=y3,col=col3,group=1)) +
  geom_line(aes(x=x, y=y4,colour=col4, group=1)) +
  geom_line(aes(x=x, y=y4,col=col4,group=1)) +
  scale_color_manual(breaks = c("blue","orange","cyan","red","black"),
                     values=c("blue" = "blue", "red" = "red","orange" = "orange","cyan" = "cyan","black" = "black"),
                     labels=c("s1on","s2_on","s1_off","s2off","new"),name="")

You can use breaks to reorder the legend. 您可以使用breaks来重新排列图例。 The values argument is mapping the right color to the right value. values参数将正确的颜色映射到正确的值。

It is easier for me to see if the values in df are different than the colors you want 对我来说,更容易查看df中的值是否不同于所需的颜色

df <- data.frame(x = 1:100,
             y1 = rnorm(100,1,100),
             y2=rnorm(100,5,50),
             y3=rnorm(100,10,500),
             y4=rnorm(100,1,200),
             col1 = c(rep("rrr", 50), 
                      rep("bbb", 10), 
                      rep("rrr", 40)),
             col2=c(rep("blbl", 50), 
                    rep("bbb", 10), 
                    rep("blbl", 40)),
             col3=c(rep("ooo", 50),
                    rep("bbb", 10), 
                    rep("ooo", 40)),
             col4=c(rep("ccc", 50), 
                    rep("bbb", 10), 
                    rep("ccc", 40)))

ggplot(df, aes(x=x, y=y1)) + 
  geom_line(aes(colour=col1, group=1)) +
  geom_line(aes(x=x, y=y2,col=col2,group=1)) +
  geom_line(aes(x=x, y=y3,col=col3,group=1)) +
  geom_line(aes(x=x, y=y4,colour=col4, group=1)) +
  geom_line(aes(x=x, y=y4,col=col4,group=1)) +
  scale_color_manual(breaks = c("blbl","ooo","ccc","rrr","bbb"),
                 values=c("blbl" = "blue", "rrr" = "red","ooo" = "orange","ccc" = "cyan","bbb" = "black"),
                 labels=c("s1on","s2_on","s1_off","s2off","new"),name="")

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

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