<jupyter_start><jupyter_text>Altair ContinuedWe last looked at the US Employment dataset, and created some starter visualizations. This time, we'll be analyzing and visualizing the [palmerpenguins](https://github.com/allisonhorst/palmerpenguins) dataset, which is a great starter dataset! More information can be found by following the link (including images of cute penguins.) Palmer PenguinsYou will have to run `pip3 install palmerpenguins` in order to have access to the dataset. If you are using Google Colab, you can simply run the following code cell.<jupyter_code>#!pip install palmerpenguins
import altair as alt
from palmerpenguins import load_penguins

penguins = load_penguins()
penguins.sample(5)<jupyter_output><empty_output><jupyter_text>Data ExplorationUsing the methods from the previous worksheet, answer the following questions about the palmerpenguins dataset. Question 1: Which species of penguins are represented in this dataset?<jupyter_code># Write your answer to question 1 here<jupyter_output><empty_output><jupyter_text>Question 2: On average, which species of penguin has the longest beak?<jupyter_code># Write your answer to question 2 here<jupyter_output><empty_output><jupyter_text>Question 3: Describe the relationship between flipper length and bill length.<jupyter_code># Write your answer to question 3 here<jupyter_output><empty_output><jupyter_text>Question 4: Create a scatterplot for flipper length vs bill length. Each species should be a different color.<jupyter_code># Write your answer to question 4 here<jupyter_output><empty_output><jupyter_text>Question 5: Compare and describe the distribution of flipper lengths across the different species.<jupyter_code># Write your answer to question 5 here<jupyter_output><empty_output><jupyter_text>More Altair!Let's start off by visualizing the relationship between bill length and bill depth. Notice the use of `.interactive()` after we've defined the marks and channels. This allows the user to scroll and pan through the visualization -- since this is such a common request, Altair provides it by default!<jupyter_code>alt.Chart(penguins).mark_point().encode(
    x="bill_length_mm:Q",
    y="bill_depth_mm:Q"
).interactive()
# Now let's color-code the species
species_color = alt.Color("species:N") # We can extract this out into a separate variable

alt.Chart(penguins).mark_point().encode(
    x="bill_length_mm:Q",
    y="bill_depth_mm:Q",
    color=species_color
).interactive()
# Now to create our own legend (this will come in handy soon!)
species_color = alt.Color("species:N", legend=None) # Remove the default legend

# Create scatterplot of bill length vs bill depth
bills = alt.Chart(penguins).mark_point().encode( # Notice we create a new Chart variable
    x="bill_length_mm:Q",
    y="bill_depth_mm:Q",
    color=species_color
).interactive()

legend = alt.Chart(penguins).mark_rect().encode( # We also create a legend variable (it's a mini viz)
    y=alt.Y("species:N", axis=alt.Axis(orient="right")),
    color=species_color # Reusing the species_color variable -- this is why we created it!
)

bills | legend # It's this easy to mash visualizations together<jupyter_output><empty_output><jupyter_text>Other InteractionsWhat if you want to do more than just panning and zooming? Then you'll need to understand how Altair represents interactions. More information can be found [at the documentation here](https://altair-viz.github.io/user_guide/interactions.html). The next few examples are based on the documentation. Selections and ConditionsYou must first identify a `selection`; this allows a viewer to interact with and select specific parts of your visualization.Then, you have to identify a `condition` that changes depending on what is being selected. A Simple ExampleHere's an example of a rectangular selection -- the user is allowed to click and drag on the graph (the `selection`), and the color of the dots will change depending on whether or not it is inside the selection (the `condition`).<jupyter_code>selection = alt.selection_interval() # Use a rectangular selection

species_color = alt.condition(selection,    # Set the color to change depending on a the selection
                              alt.Color("species:N", legend=None),
                              alt.value("lightgray"))

# Create scatterplot of bill length vs bill depth
bills = alt.Chart(penguins).mark_point().encode(
    x=alt.X("bill_length_mm:Q", scale=alt.Scale(zero=False)),
    y=alt.Y("bill_depth_mm:Q", scale=alt.Scale(zero=False)),
    color=species_color
).add_selection( # We have to tell the chart to use the selection we've defined
    selection
)

# Create corresponding legend for species
legend = alt.Chart(penguins).mark_rect().encode(
    y=alt.Y("species:N", axis=alt.Axis(orient="right")),
    color=species_color
)

bills | legend<jupyter_output><empty_output><jupyter_text>A More Complicated ExampleWhat if you wanted to allow the viewer to click on a species to see all the corresponding points? Examine the code below while thinking about what the *selection* and *condition* are.<jupyter_code>selection = alt.selection_multi(fields=['species']) # A different kind of selection!

species_color = alt.condition(selection,    # Set the color to change depending on a the selection
                              alt.Color("species:N", legend=None),
                              alt.value("lightgray"))

# Create scatterplot of bill length vs bill depth
bills = alt.Chart(penguins).mark_point().encode(
    x=alt.X("bill_length_mm:Q", scale=alt.Scale(zero=False)),
    y=alt.Y("bill_depth_mm:Q", scale=alt.Scale(zero=False)),
    color=species_color
).interactive()

# Create corresponding legend for species
legend = alt.Chart(penguins).mark_rect().encode(
    y=alt.Y("species:N", axis=alt.Axis(orient="right")),
    color=species_color
).add_selection(selection) # We now add it to the legend instead, since that is what the viewer interacts with

bills | legend<jupyter_output><empty_output><jupyter_text>Your Turn to PracticeLook through the above examples and documentation! **Make sure you read carefully through my code!** These will be good references. Practice 1: Visualize the relationship between flipper length and body mass. Allow the user to filter by species.<jupyter_code># Write your code for Practice 1 here.<jupyter_output><empty_output><jupyter_text>Practice 2: Visualize the relationship between island and body mass. Choose appropriate marks, channels, and interactions!<jupyter_code># Write your code for Practice 2 here.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08481262327416174, 'fraction_numerical': 0.0024275527234107115, 'mean_word_length': 5.236518448438979, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '667051', 'license': 'MIT', 'n_tokens_mistral': 1903, 'n_tokens_neox': 1825, 'n_words': 693, 'path': 'art of data/topics/data viz/archive/altair_cont.ipynb', 'repo_name': 'lee-edu/materials', 'size': '6591'}