data = DataFrame(x=1:5,y=1:5,color=["a","a","b","a","c"])
using StatsPlots
@df data scatter(:x,:y)
How can add color to the plot using the discrete value of column :color automatically?
In ggplot I can do something like
# how to add color based on column :color
@df data scatter(:x,:y, color=:color, palettee= :Spectral)
Which doesn’t work in Julia.
I don’t want to assign color values to the column :color manually. I just want to select a palette and let the package chose the discrete color values automatically.
OK, a long time passed and the code snippet was kept in the fridge and maybe it will have to be reheated. Note the manual adjustment of colorbar label spacing.
using DataFrames, StatsPlots, LaTeXStrings; pgfplotsx()
df = DataFrame(x=1:6, y=1:6, color=["a","a","b","a","c","g"])
dict = Dict(c => i for (i, c) in pairs(unique(df.color)))
n = length(dict)
@df df scatter(:x, :y, zcolor=get.(Ref(dict),df.color,NaN), color=palette(:roma,n+1), label=false,
ms=7, clims=(1,n+1), colorbar_tickfontcolor=:white, colorbar_title=join(unique(df.color), L"\ "^15),
)
If you are familiar with plotting tools in another language and like them, there’s nothing wrong with using them from Julia. The interop story with Python is great, and I just continue plotting with matplotlib and altair (the latter for stats + interactivity) from Julia as I did before from Python. The syntax is almost always literally the same. If RCall.jl is also similarly seamless - I have no idea - then using ggplot directly can be just as feasible.