ggplot2:如何调整线条类型+图例中的顺序? [英] ggplot2: how to adjust line types + order in legend?

查看:801
本文介绍了ggplot2:如何调整线条类型+图例中的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调整下面的ggplot中的线型。因此,我在data.frame df中引入了另一列来表示行类型,但是一旦将其转换为一个因子,图例中将显示linetype而不是method(请参见试用版3)。



如何在图例中获得方法?最后,我希望能够自由选择线型

  • 自由选择


    1. 这些线型出现在图例中的顺序,并且
    2. 具有相应的方法,如图例文本所示。


    $ b

      require(ggplot2)
    set.seed(1)
    df < - data.frame(x = c(1:4,2:5),
    method = rep(c(a,b),each = 4),
    lt = rep(c(5,3),each = 4),
    value = rep(c(0,1),each = 4)+ runif(8))

    ## trial 1:
    ggplot(df,aes(x = x,y = value))+
    geom_point()+
    geom_line(aes(group = method,linetype = method ))
    #很好,但不是线型我想有

    ##试用版2:
    ggplot(df,aes(x = x,y = value)) +
    geom_point()+
    geom_line(aes(group = method,linetype = lt))
    #正确的线型,但没有传说

    ##试用版3:
    ggplot(df,aes(x = x,y = value))+
    geom_point()+
    geom_line(aes(group = method,linetype = as.factor(lt)))
    #legend,但不是正确的(我想要 group在图例中加入
    #variablemethod,如试验1)


    解决方案使用方法作为 linetype ,然后手动将其映射到你想要的线。

      ggplot(df,aes(x = x,y = value) )+ 
    geom_point()+
    geom_line(aes(linetype = method))+
    scale_linetype_manual(breaks = c(a,b),values = c(5,3 ))


    I would like to adjust the linetypes in the following ggplot. I thus introduce another column in the data.frame df to represent the line type, but once I convert it to a factor the linetype instead of "method" appears in the legend... (see trial 3).

    How can I get "method" in the legend? In the end I would like to be able to

    1. freely choose the linetype,
    2. freely choose the order in which these linetypes appear in the legend, and
    3. have the corresponding "method" shown as legend text.

    Here are my attempts:

    require(ggplot2)
    set.seed(1)
    df <- data.frame(x=c(1:4, 2:5),
                     method=rep(c("a", "b"), each=4),
                     lt=rep(c(5,3), each=4),
                     value=rep(c(0,1), each=4)+runif(8))
    
    ## trial 1:
    ggplot(df, aes(x=x, y=value)) + 
       geom_point() + 
       geom_line(aes(group=method, linetype=method)) 
    # fine, but not the linetypes I would like to have
    
    ## trial 2:
    ggplot(df, aes(x=x, y=value)) + 
       geom_point() + 
       geom_line(aes(group=method, linetype=lt)) 
    # correct linetypes, but no legend
    
    ## trial 3:
    ggplot(df, aes(x=x, y=value)) + 
       geom_point() + 
       geom_line(aes(group=method, linetype=as.factor(lt))) 
    # legend, but not the correct one (I would like to have the "group"ing 
    # variable "method" in the legend as in trial 1)
    

    解决方案

    Use method as the linetype, but then manually map it to the types of lines you want. You don't need to introduce another variable this way.

    ggplot(df, aes(x=x, y=value)) +
        geom_point() +
        geom_line(aes(linetype=method)) +
        scale_linetype_manual(breaks=c("a","b"), values=c(5,3))
    

    这篇关于ggplot2:如何调整线条类型+图例中的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
  • 登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆