Playing around the Cartesian plane with the R programming language

Abrar Shariar
N-polygon
Published in
4 min readSep 9, 2019

--

I love visualization. For many of us who are visual learners, visualization helps us learn new concepts easily and retain them efficiently. But nothing beats a visualization that you do on your own, right? Although with a few strokes of the keyboard you can find the graphs of any of the following equations online, in this article we will gradually build up some interesting graphs from their equations and recognize their pattern eventually. Let’s get our hands dirty with a few lines of the R programming language and conquer the Cartesian plane!

Just FYI: You don’t need to worry about learning the nitty-gritty details of R. The functions used are quite intuitive, once you get hold of the underlying mathematical notion.

y = x²

Let’s start with the parent quadratic function. Imagine you don’t have any idea how this equation looks on the x-y plane (or you want to get a visualization to understand better). If you don’t have R installed on your machine, you might want to grab it from here. Once you have it let’s write some simple lines:

Here we take the domain as [-50,50] and plot the graph of the function y=x². Notice, in the plot function we pass the type=”l” which is a way of saying we want to see lines type of graph and we set the color as red with col=”red”. The xlim and ylim are used to set the range of values for the x and y — axis respectively. Next we next a horizontal and vertical line with abline, in case we want to see the four quadrants separated by the x-y axis. Next we set the title, its color and font size and we are good to go!

Save it as a xyz.R file and let’s run it:

> Rscript xyz.R

The graph generated will look like these:

graph of the equation y=x²

As we know, y=x² is indeed a parabola! Let’s try some more.

y=x²-500

Notice here we are diminishing the resultant value of y by 500. Intuitively this would mean our previous parabola of the equation y=x² is reduced by the value of 500, so the projected graph would go vertically down by 500. Let’s change a few lines of code from the previous equation:

Let’s see how the graph looks like:

graph of the equation y=x²-500

Our conjecture is indeed correct. The parabola has gone vertically downwards by 500 units along the y-axis.

y = (x+20)²

y=(x+20)²

Here, we observe that the graph of y=(x+20)² is essentially the parabola denoted by y=x² moved 20 units along the negative x-axis. Likewise, if we had y=(x-20)², it would move the parabola along the positive x-axis by 20 units.

Some more with parabolas:

y = 5-(x+20)²

y=5-(x+20)²

[Study on the shapes of parabolas based on equation]

Let’s look at some straight line examples now.

y = 500-(900*x)

y = 500 — (900*x)

So, this is a straight line where we can see the equation in the slope-intercept form of y=mx+c, where m is the slope and c is the y-intercept. Notice that in the case of the aforementioned equation:

  • m = -900
  • c = 500

We have a rise/run or slope of -900 units i.e for every unit we move to the right along the x-axis, we go vertically down (in negative y-axis) by 900 units.

Reducing the domain might help understand it better:

y = 10–5x

Observe that the y-intercept = 10 i.e. the line intercepts the y-axis at the point (0,10). Also, the slope = -5 i.e. every unit progression we make in the x-axis positive direction, we go the negative y-axis by 5 units.

--

--