Faceting Linear Regression
Set Up
Before Starting make sure to have Observable-Jupyter and any other needed libraries installed in your local environment.
[8]:
from observable_jupyter import embed
from palmerpenguins import load_penguins
import pandas as pd
import json
Load and Format Data
[9]:
penguins_data = load_penguins()
penguins_data.head()
[9]:
| species | island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex | year | |
|---|---|---|---|---|---|---|---|---|
| 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 |
The following block of code structures the data into a format accepted by Observable.
[10]:
result = penguins_data.to_json(orient="records")
parsed = json.loads(result)
data = json.dumps(parsed, indent=4)
Formated_Data = json.loads(data)
Embed your data into the visualization
The Faceting Linear Regression visualization consists of one cell:
Facet_Regression_Chart : Desplays the graph.
To make your visualization work you will need to access the input variables:
csv_data : set equal to your structured data.
x_variable : set to the title of the column containg the data you want on the x axis.
y_variable : set to the title of the column containg the data you want on the y axis.
groupings : set to the name of the column in your data that allows you to difrerentiate your data.
faceting_variable : set to the column name you want to use to facet your data.
[11]:
embed(
'@rstorni/plot-regression-demo',
cells=['Facet_Regression_Chart'],
inputs = {
"csv_data" : Formated_Data,
"x_variable" : "bill_length_mm",
"y_variable" : "body_mass_g",
'groupings' : 'species',
'faceting_variable' : 'island'
}
)