【问题标题】:reorder legend of multiple line plot with segment using ggplot2使用 ggplot2 重新排序带段的多线图的图例
【发布时间】:2018-11-12 00:10:07
【问题描述】:

我有一个多线图,每条线的一部分以与讨论相同的方式突出显示 here。 这是可重现的示例:

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="")

这给出了图例中项目的顺序,如下图所示。我想重新排序它们。由于每条线都有一个段并且每个段都有相同的颜色“黑色”,因此我无法在scale_color_manual 中使用breaks 参数,因为它需要5 个值,并且该图是为数据框中的4 列绘制的。如何按 s1on、s2on、s1_off、s2_off 和 new 的顺序重新排列图例中的项目?

【问题讨论】:

    标签: r ggplot2 legend line-plot


    【解决方案1】:

    这可能就是你要找的东西

     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="")
    

    您可以使用breaks 重新排序图例。 values 参数将正确的颜色映射到正确的值。

    我更容易查看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="")
    

    【讨论】:

    • 啊..没那么难!我没有想到我可以这样定义它。非常感谢!
    • 谢谢你,太感谢了。
    猜你喜欢
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    相关资源
    最近更新 更多