From e4511949b7c264ce60392398d772f5a5c8b1cad3 Mon Sep 17 00:00:00 2001 From: Rommel Artola <121839761+RommelArtola@users.noreply.github.com> Date: Wed, 14 Aug 2024 21:52:45 -0700 Subject: [PATCH] Update Scatterplots.md Added a small bit about doing the same seaborn scatterplot in a seaborn.object plot with some added things like renaming the plot, and setting the theme to all white to get started. Also added a linear fit line so users can play with it and see the art of the possible with it! Trying to figure out how to add the photo now to this locked repo using plot.save('seaborn_objects_scatter.png', bbox_inches='tight') --- Presentation/Figures/Scatterplots.md | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Presentation/Figures/Scatterplots.md b/Presentation/Figures/Scatterplots.md index 11b0d32a..4360dd57 100644 --- a/Presentation/Figures/Scatterplots.md +++ b/Presentation/Figures/Scatterplots.md @@ -57,6 +57,41 @@ This results in: ![seaborn_scatterplot](Images/Scatterplots/seaborn_scatter.png) +Alternatively, you can also use the seaborn.objects library which is more built around the grammar of graphics, by layering objects. +```python +import seaborn.objects as so +from seaborn import load_dataset + +# Load the tips dataset +df = load_dataset("tips") + +""" +Below we'll make a simple scatter plot with a linear trendline fit to the +scatter using seaborn.objects + +Comments are provided on each line to help call out what each line is doing +""" + +# Initialize the plot instance +plot = (so.Plot(data=df, x='total_bill', y='tip', color='time') #General aesthetic variables + .add(so.Dots(pointsize=5)) # Layer dots (scatter) aesthetic with size 5 for the dots + .add(so.Line(), so.PolyFit(order=1)) # Layer a trendline with order 1 (linear) + .label(title='Total Bill Due vs Tip Amount (by Time)', # Manually set the labels + x='Total Bill Due (in dollars)', + y='Tip Amount (in dollars)', + color='Time') + .theme({"axes.facecolor": "w"}) # Remove spines and make the entire chart area white + ) + + +plot.plot() #Call the plot +``` +This results in: + +![seaborn_objects_dotplot](Images/Scatterplots/seaborn_objects_scatter.png) + + + ## R In R, one of the best tools for creating scatterplots is the function `ggplot()`, found in the `ggplot2` package. For this demonstration, we will also be using a dataset already built in to R called `mtcars`.