Categories
data science

Plotting an equation – R

This is how you plot an equation in R.

The equation we are going to plot is a simple one.

y = x– 1

The first thing we do is set the value for x by creating a sequence from 0 to 1 at 0.01 intervals

> x <- seq(0,1,by=0.01)

Now we can get the y values

> y=x^-1

so if we look at both x and y, we should get the following values

> x
  [1] 0.00 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09
 [11] 0.10 0.11 0.12 0.13 0.14 0.15 0.16 0.17 0.18 0.19
 [21] 0.20 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28 0.29
 [31] 0.30 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39
 [41] 0.40 0.41 0.42 0.43 0.44 0.45 0.46 0.47 0.48 0.49
 [51] 0.50 0.51 0.52 0.53 0.54 0.55 0.56 0.57 0.58 0.59
 [61] 0.60 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69
 [71] 0.70 0.71 0.72 0.73 0.74 0.75 0.76 0.77 0.78 0.79
 [81] 0.80 0.81 0.82 0.83 0.84 0.85 0.86 0.87 0.88 0.89
 [91] 0.90 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99
[101] 1.00
> y
  [1]        Inf 100.000000  50.000000  33.333333
  [5]  25.000000  20.000000  16.666667  14.285714
  [9]  12.500000  11.111111  10.000000   9.090909
 [13]   8.333333   7.692308   7.142857   6.666667
 [17]   6.250000   5.882353   5.555556   5.263158
 [21]   5.000000   4.761905   4.545455   4.347826
 [25]   4.166667   4.000000   3.846154   3.703704
 [29]   3.571429   3.448276   3.333333   3.225806
 [33]   3.125000   3.030303   2.941176   2.857143
 [37]   2.777778   2.702703   2.631579   2.564103
 [41]   2.500000   2.439024   2.380952   2.325581
 [45]   2.272727   2.222222   2.173913   2.127660
 [49]   2.083333   2.040816   2.000000   1.960784
 [53]   1.923077   1.886792   1.851852   1.818182
 [57]   1.785714   1.754386   1.724138   1.694915
 [61]   1.666667   1.639344   1.612903   1.587302
 [65]   1.562500   1.538462   1.515152   1.492537
 [69]   1.470588   1.449275   1.428571   1.408451
 [73]   1.388889   1.369863   1.351351   1.333333
 [77]   1.315789   1.298701   1.282051   1.265823
 [81]   1.250000   1.234568   1.219512   1.204819
 [85]   1.190476   1.176471   1.162791   1.149425
 [89]   1.136364   1.123596   1.111111   1.098901
 [93]   1.086957   1.075269   1.063830   1.052632
 [97]   1.041667   1.030928   1.020408   1.010101
[101]   1.000000

So now we can plot these values as a line graph by adding the type='l' option.

> plot(x,y,type='l')

which should give us this

graph

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s