KNOWLEDGE BASE

How to Divide a Scatter Plot With Any Linear Line


Published: 06 Nov 2023
Last Modified Date: 08 Nov 2023

Question

How to divide a scatter plot with any linear line.

For example, in the below scatter plot, how to manually input 2 dots and divide the it into 2 parts with color in the mark or in the background.

User-added image

Environment

  • Tableau Desktop
  • Tableau Server
  • Tableau Cloud

Answer

Please consider the below options to divide the scatter plot.  There is a sample packaged workbook and videos to follow along with the solution in the Attachments section of this article.

Option 1

Make a Linear line from the input dots and color the marks.
The steps are as follows

1. Create 4 parameters as [X1], [Y1], [X2], [Y2] to input 2 dots for the linear line.
2. Create a Slope calculation using the 4 Parameters.
AVG([Y2]-[Y1])/AVG([X2]-[X1])
3. Create a Linear line function to divide the marks into two parts -- "IN" and "OUT".
IF SUM([Quantity])>=[! Slope]*SUM([Sales])+([Y2]-[! Slope]*[X2]) THEN "IN"
ELSEIF SUM([Quantity])<[! Slope]*SUM([Sales])+([Y2]-[! Slope]*[X2]) THEN "OUT"
END
4. Add the calculation in Step 3 to [Color] Mark.

The worksheet [Option 1] and process video [divide option 1.mp4] are attached.
Please check the below images.
User-added image

Option 2

Make a Linear line from the input dots and color the backgrounds.
The steps are as follows

1. Create 4 parameters as [X1], [Y1], [X2], [Y2] to input 2 dots for the linear line.
2. Create a Slope calculation using the 4 Parameters.
AVG([Y2]-[Y1])/AVG([X2]-[X1])
3. Create a calculation to calculate the Y of the Linear line graph.
IF INDEX()%2 = 0 THEN AVG([Y1])
ELSEIF INDEX()%2 = 1 THEN AVG([Y2])
END
4. Create a Linear line function. 
SUM([Sales])*[! Slope]+WINDOW_MIN([! Y])
5. Add the calculation in Step 4 to [Rows] Shelf and set it as a dual-axis.
6. Set the Mark Type of the calculation in Step 4 as [Area] and delete the Measure Names in this shelf.

The worksheet [Option 2] and process video [divide option 2.mp4] are attached.
Please check the below images.
User-added image
Did this article resolve the issue?