Scatter plots with different colors depending on flower species

Hi guys! :smiley:

I am new to ggplot2 and would appreciate a little help please :slight_smile:

I am playing around with famous IRIS flower dataset.
This code is working great in Python 3 script.

c_col = ["#2f4858", "#f6ae2d", "#f26419",
         "#33658a", "#55dde0", "#2f4858",
         "#2f4858", "#f6ae2d", "#f26419",
         "#33658a", "#55dde0", "#2f4858"]

fig = (
    ggplot(data.dropna(subset = ['sepal_length'])) +
    geom_point(
        aes(x = 'sepal_length',
            y = 'sepal_width'),
        fill = c_col[10], color = 'blue'
    ) +
    labs(
        title ='Relationship between sepal_length and sepal_width',
        x = 'sepal_length',
        y = 'sepal_width',
    ))

I was wondering how to add a feature to plot three different colors depending on flower species... above code plots all three flower species in BLUE color... but I would like to have three colors in my Scatered diagram... so that every flower specie is in its own color.

Waiting your reply!
Many, many thanks!

Ivan :smiley:

Here is an example based on the iris dataset (you appear to have something based on it):

ggplot(iris) + 
  geom_point(
    aes(x = Sepal.Length,
        y = Sepal.Width, 
        colour = Species)
  ) +
  labs(
    title ='Relationship between sepal_length and sepal_width',
    x = 'sepal_length',
    y = 'sepal_width'
  )

Thanks Martin.

I tried above code, but does not work from Python script.

I am using 'plotnine' pip lib to be able to run ggplot2 from Python script.

So syntax is a little different.

Ok, I didn't read the Python bit.

Assuming it works the same as R's ggplot2, then just move the colour bit into the aes() function to vary the species by colour. You'll also want scale_colour_manual() to pick from your list of colours..

If you're using the Python implementation of ggplot2 (ggplot) http://ggplot.yhathq.com/ in Python, you might have better luck asking on Stack Overflow or a Python forum, if no one has the answers for you here.

There's a notebook full of examples in the repo here:

2 Likes

Hi Martin... does not work.

I will play around with an additional "key-value" R lib... to see what I can achieve :slight_smile:

Ivan

Hi Martin,

I found solution here that works, provided by Dr. Gregor Scheithauer:

Data visualization in Python

data['ounces_str'] = data['ounces']
data['ounces_str'] = data['ounces_str'].apply(str)
fig = (
    ggplot(data.dropna(subset = ['abv'])) +
    geom_point(
        aes(x = 'abv',
            y = 'ibu',
            fill = 'ounces_str'),
        alpha = 0.5,
        color = 'black'
    ) +
    labs(
        title ='Relationship between alcoholic content (abv) and int. bittering untis (ibu)',
        x = 'abv - The alcoholic content by volume',
        y = 'ibu - International bittering units',
    ) +
    scale_fill_manual(
        name = 'Ounces',
        values = c_col) +
    scale_x_continuous(
        limits = (0, 0.14),
        labels = labels(0, 0.14, 0.02),
        breaks = breaks(0, 0.14, 0.02)
    )  +
    scale_y_continuous(
        limits = (0, 150),
        labels = labels(0, 150, 30),
        breaks = breaks(0, 150, 30)
    )
)

Thank you for your help.
Ivan

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.