2011-12-06 15 views
9

以下のggplotで線種を調整したいと思います。私はdata.frame dfの中に別の列を導入して線種を表現しますが、それを因子に変換すると、 "method"の代わりに線種が凡例に現れます(試行3参照)。ggplot2:行の種類を調整する方法+凡例の順序は?

凡例で「方法」を取得するにはどうすればよいですか?最後に、私は自由に自由にこれらの線種は、凡例に表示される順序を選択し、

  • 凡例として示す対応する「メソッド」を有する
  • 、線種を選択

    1. できるようにしたいと思いテキスト。ここで

    私の試みは、以下のとおりです。linetypeとして

    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) 
    
  • +0

    '+ scale_linetype(名= "法")'私の伝説のタイトル "メソッド" を与える裁判3 – Chase

    +0

    で動作するようですが、それでも私は "3" と「5を取得します試行1のように "a"と "b"の代わりに伝説のテキストとして –

    答えて

    16

    使用methodが、その後、手動であなたが望むラインの種類にマップします。このように別の変数を導入する必要はありません。

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

    enter image description here

    関連する問題