The data comes from the Palmer Penguins project .
License: CC0 (Creative Commons Zero)
Citation: Horst AM, Hill AP, Gorman KB (2020). palmerpenguins: Palmer Archipelago (Antarctica) penguin data . R package version 0.1.0. doi: 10.5281/zenodo.3960218.
import altair as alt
from palmerpenguins import load_penguins
penguins = load_penguins()
penguins.head()
0
Adelie
Torgersen
39.1
18.7
181.0
3750.0
male
2007
1
Adelie
Torgersen
39.5
17.4
186.0
3800.0
female
2007
2
Adelie
Torgersen
40.3
18.0
195.0
3250.0
female
2007
3
Adelie
Torgersen
NaN
NaN
NaN
NaN
NaN
2007
4
Adelie
Torgersen
36.7
19.3
193.0
3450.0
female
2007
I first compared the average body mass of each species.
penguins.groupby("species" )["body_mass_g" ].mean()
species
Adelie 3700.662252
Chinstrap 3733.088235
Gentoo 5076.016260
Name: body_mass_g, dtype: float64
Gentoo penguins have the highest average body mass of the three species.
Next, I looked at the relationship between flipper length and body mass.
alt.Chart(penguins).mark_point().encode(
x= alt.X(
"flipper_length_mm:Q" ,
scale= alt.Scale(domain= [170 , 235 ]),
title= "Flipper length (mm)"
),
y= alt.Y(
"body_mass_g:Q" ,
scale= alt.Scale(domain= [2500 , 6500 ]),
title= "Body mass (g)"
),
color= "species:N"
).properties(
width= 600 ,
height= 400
)
Figure 1: Relationship between flipper length and body mass across penguin species.
The plot shows that penguins with longer flippers tend to have greater body mass.
Finally, I calculated the correlation between flipper length and body mass for each species.
(penguins
.groupby("species" )
.apply ( lambda x: x["flipper_length_mm" ].corr(x["body_mass_g" ]))
)
species
Adelie 0.468202
Chinstrap 0.641559
Gentoo 0.702667
dtype: float64
The correlations are positive for all three species, showing that the relationship between longer flippers and greater body mass is also present within each species, not just across the full dataset.