2017-09-25 37 views
0

以前にカスタム線種と色を指定したプロットにカスタム凡例ラベルを設定する際に問題があります。同様の質問がlinetypes and line colorsのカスタム凡例を扱うstackoverflowにあります。しかし、上記の2つの属性の上に凡例ラベルを指定すると、ggplot2は一見破壊され始めます。 2つの別々の伝説でscale_linetype_manual()またはscale_color_manual()結果が作成されているいずれかのラベルを設定する次のグラフggplot凡例の線種、色、ラベルの制御

Setting labels in either scale_linetype_manual or scale_color_manual results in two legends being created.

library(ggplot2) 

df <- data.frame(x = rep(1:2, 2), y = c(1, 3, 2, 2), 
      treatment = c(rep("one", 2), rep("two", "2"))) 

ggplot(df, aes(x = x, y = y, colour = treatment, linetype = treatment)) + 
    geom_line() + 
    scale_linetype_manual(values = c(1, 3), 
         labels = c("Treatment one", "Treatment two")) + 
    scale_color_manual(values = c("black", "red")) 

上記のコードの結果:

は、ここに私の最小限の再現性の例です。線の正しい破線を持つものは、常に無色になります。一方、正しい色を持つものは線種を正しく表現できません(実線のみ)。

ggplot2で色、ラインタイプ、凡例のラベルを一度に制御する方法はありますか?あるいは、これはgetgoのデータフレームでラベルを正しく指定するだけのパッケージの制限ですか?

答えて

2

scale_linetype_manual()scale_color_manual()のラベルを同じにします。

library(ggplot2) 

df <- data.frame(x = rep(1:2, 2), y = c(1, 3, 2, 2), 
       treatment = c(rep("one", 2), rep("two", "2"))) 

ggplot(df, aes(x = x, y = y, colour = treatment, linetype = treatment)) + 
    geom_line() + 
    scale_linetype_manual(values = c(1, 3), 
         labels = c("Treatment one", "Treatment two")) + 
    scale_color_manual(values = c("black", "red"), 
        labels = c("Treatment one", "Treatment two")) 

enter image description here

関連する問題