Plot 如何使用Gadfly';茱莉亚的《比例·色彩·离散·手册》?

Plot 如何使用Gadfly';茱莉亚的《比例·色彩·离散·手册》?,plot,syntax,julia,gadfly,Plot,Syntax,Julia,Gadfly,我导入了一个数据帧,如下所示: 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.182

我导入了一个
数据帧
,如下所示:

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\u discrete\u manual
设置颜色,并尝试使用
Guide.manual\u color\u key
更改颜色键图例。然而,牛虻制作了两个颜色键

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

我的问题是如何在使用
Scale.color\u discrete\u manual
时更改颜色键图例


一个相关的问题是,最佳答案建议使用两层加上
Guide.manual\u color\u key
。有没有更好的解决方案来使用
数据帧
缩放。彩色离散手册

目前,用户似乎无法根据

来自同一来源,建议使用额外的列作为“标签”。虽然改变颜色键不是“自然的”,但效果相当不错

因此,对于问题中的同一数据集。我们再增加一列:

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\u discrete\u manual

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