<jupyter_start><jupyter_text>Facet wrap`facet_wrap()` creates a collection of plots (facets), where each plot is differentiated by the faceting variable. These plots are wrapped into a certain number of columns or rows as specified by the user.<jupyter_code>mpg.head()<jupyter_output><empty_output><jupyter_text>Basic scatter plot:<jupyter_code>(
    ggplot(mpg, aes(x='displ', y='hwy')) 
    + geom_point()
    + labs(x='displacement', y='horsepower')
)<jupyter_output><empty_output><jupyter_text>Facet a discrete variable using `facet_wrap()`:<jupyter_code>(
    ggplot(mpg, aes(x='displ', y='hwy')) 
    + geom_point()
    + facet_wrap('class')
    + labs(x='displacement', y='horsepower')
)<jupyter_output><empty_output><jupyter_text>Control the number of rows and columns with the options `nrow` and `ncol`:<jupyter_code># Selecting the number of columns to display
(
    ggplot(mpg, aes(x='displ', y='hwy')) 
    + geom_point()
    + facet_wrap('class',
                 ncol = 4 # change the number of columns
                )
    + labs(x='displacement', y='horsepower')
)
# Selecting the number of rows to display

(
    ggplot(mpg, aes(x='displ', y='hwy')) 
    + geom_point()
    + facet_wrap('class', 
                 nrow = 4 # change the number of columns
                )
    + labs(x='displacement', y='horsepower')
)<jupyter_output><empty_output><jupyter_text>To change the plot order of the facets, reorder the levels of the faceting variable in the data.<jupyter_code># re-order categories
mpg['class'] = mpg['class'].cat.reorder_categories(['pickup', 'suv','minivan','midsize','compact','subcompact','2seater'])
# facet plot with reorded drv category
(
    ggplot(mpg, aes(x='displ', y='hwy')) 
    + geom_point()
    + facet_wrap('class')
    + labs(x='displacement', y='horsepower')
)<jupyter_output><empty_output><jupyter_text>Ordinarily the facets are arranged horizontally (left-to-right from top to bottom). However if you would prefer a vertical layout (facets are arranged top-to-bottom, from left to right) use the `dir` option:<jupyter_code># Facet plot with vertical layout
(
    ggplot(mpg, aes(x='displ', y='hwy')) 
    + geom_point()
    + facet_wrap('class'
                , dir = 'v' # change to a vertical layout
                )
    + labs(x='displacement', y='horsepower')
)<jupyter_output><empty_output><jupyter_text>You can choose if the scale of x- and y-axes are fixed or variable. Set the `scales` argument to `free-y`, `free_x` or `free` for a free scales on the y-axis, x-axis or both axes respectively. You may need to add spacing between the facets to ensure axis ticks and values are easy to read.A fixed scale is the default and does not need to be specified.<jupyter_code># facet plot with free scales
(
    ggplot(mpg, aes(x='displ', y='hwy')) 
    + geom_point()
    + facet_wrap('class'
                , scales = 'free_y'           # set scales so y-scale varies with the data
                )
    + theme(subplots_adjust={'wspace': 0.25}) # add spaceing between facets to make y-axis ticks visible
    + labs(x='displacement', y='horsepower')
)<jupyter_output><empty_output><jupyter_text>You can add additional information to your facet labels, by using the `labeller` argument within the `facet_wrap()` command. Below we use `labeller = 'label_both'` to include the column name in the facet label.<jupyter_code># facet plot with labeller
(
    ggplot(mpg, aes(x='displ', y='hwy')) 
    + geom_point()
    + facet_wrap('class', labeller = 'label_both')
    + labs(x='displacement', y='horsepower')
)<jupyter_output><empty_output><jupyter_text>You can add two discrete variables to a facet:<jupyter_code># add additional column for plotting exercise
mpg["transmission"] = mpg['trans'].map(lambda x: "auto" if "auto" in x else "man" if "man" in x else "")
# inspect new column transmission which identifies cars as having an automatic or manual transmission
mpg.head()
# facet plot with two variables on one facet
(
    ggplot(mpg, aes(x='displ', y='hwy')) 
    + geom_point()
    + facet_wrap('~ class + transmission') # use ~ + to add additional faceting variables
    + labs(x='displacement', y='horsepower')
)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12824173209612183, 'fraction_numerical': 0.0014275517487508922, 'mean_word_length': 4.2880503144654085, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '82677', 'license': 'MIT', 'n_tokens_mistral': 1444, 'n_tokens_neox': 1341, 'n_words': 409, 'path': 'demo_plot/plotnine-examples/examples/facet_wrap.ipynb', 'repo_name': 'hermanzhaozzzz/DataScienceScripts', 'size': '4203'}