pythonggplot2lets-plot

How to specify `scale_color_manual()` in `python`, using `lets-plot`


My code is:

import pandas as pd
from lets_plot import *
LetsPlot.setup_html()

data = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg2.csv')

ggplot(data, aes(x="engine horsepower", y="miles per gallon")) + geom_point(aes(color="origin of car"))

And the output is: enter image description here

In R, using ggplot2, I would manually set the colors by writing:

... + scale_color_manual(values = c("US" = "red", "Asia" = "green", "Europe" = "blue")

How can I do the same in Python with lets-plot?

lets-plot reference manual for this function doesn't seem to help: here


Solution

  • Basically it's the same in lets-plot. Using a pandas Series you could create a named vector similar to R:

    import pandas as pd
    from lets_plot import *
    LetsPlot.setup_html()
    
    data = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg2.csv')
    
    ggplot(data, aes(x="engine horsepower", y="miles per gallon")) + \
        geom_point(aes(color="origin of car")) + \
        scale_color_manual(values = pd.Series(["red", "green", "blue"], 
                                              index=['US', 'Asia', 'Europe']))
    

    enter image description here