CIND123 Module 6.2 Screen Capture Hello, everyone. In this segment, we will discuss the basic plottings in R. Mainly; we will see line charts, bar charts, histograms, pie charts, and dot charts. Here is the.rmd file that is prepared for Module Six, and for each case, we will have an example [see Figure 1]. Figure 1 Let's start with line charts first. Let's define a vector "x", okay, here is our new vector, with nine values [c(1, 3, 6, 2, 8, 5, 10, 12, 13)], and if we run this command, you will see that the new variable is seen in the environment, and it is a numerical variable with nine entries [see Figure 2]. Figure 2 First, I want to introduce you the main plotting command in R, which is the plot command. "plot (x)" will give you the following graph [see Figure 3]. So this is a dot chart. You only see the points on the plotting region, okay. So here you only have the points. So, it is like for the first entry, entry number one, you have the value of "1". For entry number two, you have the value of "3", right. For entry number three, you should have the value "6". That's straightforward, for entry number three; you have the value of "6".
Figure 3 By just using the same plot command with different parameters, you can immediately go to a line chart. So here's my example for that. You take the "x" vector. When you say type- o" it will provide you the line segments, and when I say col= blue" I have the blue line segments connecting these points given for the "x" variable [see Figure 4]. Figure 4 So let's have a new variable "y" [c(2, 4, 5, 4, 12, 9, 11, 7, 10)]. "y" is a vector as well, and now I want to show you the lines command. So these lines commands will be combining the line segments for the vector "y", but this command will be providing the output on the same plot [lines(y, type= 0, pch=22, lty=2, col= red )]. So let's run it together. You will see that the new line segments are plotted on the graph that we had before [see Figure 5]. So here the parameters are, again, for "type=o", you have the line segments, and these are the different parameters that you can use. For example, here I wanted a red dashed line with square points. So "22" will go for "s" squared, for example.
Figure 5 Okay, so let's say that you want to go for a bar plot. So barplot(x, col= blue ), let's run this, and then let's have barplot(y, col= red"). Okay. So let's say that you want to have these two figures on an overall graph. So R makes it easy to combine multiple plots into one overall graph. And to do this, the simple command is "par. "par" will give you the number of rows and the number of columns. Actually, you give, okay, you write it down in terms of a vector. If you say "mfrow=c", this means that you want to have it on one row and two columns [par(mfrow=c(1, 2))]. So let's run this, and then I go to "barplot" for "x" and "barplot" for "y", okay, and R is providing these two graphs on overall one graph [see Figure 6]. Figure 6
Now, let's pass the histograms, but before passing the histograms, let's write "dev.off" here as a command, so that "par" will not be active anymore. Otherwise, while you're drawing your histograms, "par" will be still active, and you will see all your histograms in one row and two columns. Okay, so let's graph the distribution of "x" by using a histogram [hist(x)]. This is the output that you get, and by default, you have the title written as "Histogram of x," and you can also see the frequency here and the variable "x" [se Figure 7]. Figure 7 And we can change the colour if you like. We can go to blue [col= blue ]. And if you use the main parameter here [main= distribution of x ], we will see that the title is changed by that parameter. So here I have "distribution of x" as a title [see Figure 8]. Figure 8
And let's go to pie charts. "pie(x)" will provide you the pie chart [see Figure 9]. And here you see that the colours are different for each and every slice, and it is by default. It is easy to see the different slices by these different colours. Figure 9 And, when you pass to dot chart, okay, this is the graph that you will get [see Figure 10]. And it is very similar to the one that we have provided with the plot command. If you write "plot", you see that it is a dot chart, and it is very similar to the dot chart command [see Figure 3]. Figure 10 End of CIND123 Module 6 Screen Capture