2017-01-04 31 views
2

我已導入,如下一個DataFrame如何在Julia中使用Gadfly的`Scale.color_discrete_manual`改變顏色鍵?

julia> df 
100×3 DataFrames.DataFrame 
│ Row │ ex1  │ ex2  │ admit │ 
├─────┼─────────┼─────────┼───────┤ 
│ 1 │ 34.6237 │ 78.0247 │ 0  │ 
│ 2 │ 30.2867 │ 43.895 │ 0  │ 
│ 3 │ 35.8474 │ 72.9022 │ 0  │ 
│ 4 │ 60.1826 │ 86.3086 │ 1  │ 
│ 5 │ 79.0327 │ 75.3444 │ 1  │ 
│ 6 │ 45.0833 │ 56.3164 │ 0  │ 
│ 7 │ 61.1067 │ 96.5114 │ 1  │ 
│ 8 │ 75.0247 │ 46.554 │ 1  │ 
⋮ 
│ 92 │ 90.4486 │ 87.5088 │ 1  │ 
│ 93 │ 55.4822 │ 35.5707 │ 0  │ 
│ 94 │ 74.4927 │ 84.8451 │ 1  │ 
│ 95 │ 89.8458 │ 45.3583 │ 1  │ 
│ 96 │ 83.4892 │ 48.3803 │ 1  │ 
│ 97 │ 42.2617 │ 87.1039 │ 1  │ 
│ 98 │ 99.315 │ 68.7754 │ 1  │ 
│ 99 │ 55.34 │ 64.9319 │ 1  │ 
│ 100 │ 74.7759 │ 89.5298 │ 1  │ 

我想用ex1作爲x軸,ex2作爲y軸繪製該DataFrame。此外,數據按第三列:admit分類,所以我想根據:admit值給出點不同的顏色。

我用Scale.color_discrete_manual來設置顏色,我試着用Guide.manual_color_key來改變顏色的關鍵圖例。然而,事實證明牛made製造了兩個彩色鍵。

p = plot(df, x = :ex1, y = :ex2, color=:admit, 
     Scale.color_discrete_manual(colorant"deep sky blue", 
            colorant"light pink"), 
     Guide.manual_color_key("Legend", ["Failure", "Success"], 
           ["deep sky blue", "light pink"])) 

plot1

我的問題是如何使用Scale.color_discrete_manual時改變顏色關鍵傳奇?

一個相關的問題是Remove automatically generated color key in Gadfly plot,其中最好的答案建議使用兩層加Guide.manual_color_key。是否有更好的解決方案使用DataFrameScale.color_discrete_manual

回答

1

目前,它看起來像用戶不能自定義由colorScale.color_discrete_manual生成的顏色圖例基於discussion

來自相同的來源,Mattriks建議使用額外的列作爲「標籤」。雖然改變顏色鍵不是「自然的」,但它工作得很好。

因此,對於這個問題的同一數據集。我們再增加一個列:

plot(df, x = :exam1, y = :exam2, color = :admission, 
    Scale.color_discrete_manual(colorant"deep sky blue", 
           colorant"light pink")) 

plot

df[:admission] = map(df[:admit])do x 
    if x == 1 
     return "Success" 
    else 
     return "Failure" 
    end 
end 

julia> df 
100×4 DataFrames.DataFrame 
│ Row │ exam1 │ exam2 │ admit │ admission │ 
├─────┼─────────┼─────────┼───────┼───────────┤ 
│ 1 │ 34.6237 │ 78.0247 │ 0  │ "Failure" │ 
│ 2 │ 30.2867 │ 43.895 │ 0  │ "Failure" │ 
│ 3 │ 35.8474 │ 72.9022 │ 0  │ "Failure" │ 
│ 4 │ 60.1826 │ 86.3086 │ 1  │ "Success" │ 
│ 5 │ 79.0327 │ 75.3444 │ 1  │ "Success" │ 
│ 6 │ 45.0833 │ 56.3164 │ 0  │ "Failure" │ 
│ 7 │ 61.1067 │ 96.5114 │ 1  │ "Success" │ 
│ 8 │ 75.0247 │ 46.554 │ 1  │ "Success" │ 
⋮ 
│ 92 │ 90.4486 │ 87.5088 │ 1  │ "Success" │ 
│ 93 │ 55.4822 │ 35.5707 │ 0  │ "Failure" │ 
│ 94 │ 74.4927 │ 84.8451 │ 1  │ "Success" │ 
│ 95 │ 89.8458 │ 45.3583 │ 1  │ "Success" │ 
│ 96 │ 83.4892 │ 48.3803 │ 1  │ "Success" │ 
│ 97 │ 42.2617 │ 87.1039 │ 1  │ "Success" │ 
│ 98 │ 99.315 │ 68.7754 │ 1  │ "Success" │ 
│ 99 │ 55.34 │ 64.9319 │ 1  │ "Success" │ 
│ 100 │ 74.7759 │ 89.5298 │ 1  │ "Success" │ 

然後使用這個新列Scale.color_discrete_manual顏色數據