Experiment # 4
Experiment # 4
Course title: Soft Computing and Data mining Lab Total Marks: ___20_________
Practical No. 4 Date of experiment performed: ____________
Course teacher/Lab Instructor: Engr. Muhammad Usman Date of marking: ____________
Student Name:__________________________
Registration no.__________________________
Normalize
marks out of 5
(5)
Objective:
1. To be familiar with plotting
2. To be familiar with plotting functions of modern programming language R including
3. To know how to use plotting functions of modern programming language named R.
Theory:
1. Plotting:
Human beings are remarkably adept at discerning relationships from visual representations. A
well-crafted graph can help you make meaningful comparisons among thousands of pieces of
information, extracting patterns not easily found through other methods. This is one reason
why advances in the field of statistical graphics have had such a major impact on data analysis.
Data analysts need to look at their data, and this is one area where R shines. Basic plots or basic
graphs in R for data analysis include scatter plot, histogram, boxplot and image.
In this lab, we’ll review general methods for working with basic graphs or basic plotting. We’ll
also discuss how to create and save graphs in general.
2. Plotting Functions in R:
Plotting functions of modern programming language R include plot(), hist(), boxplot() and
image() These functions are discussed below along with details of how to use these functions
in R.
2.1 Scatter Plot:
The plot() function makes scatter plot, allowing you to visually inspect the trend. Here is how
to plot two vectors x and y.
x <- c(20,30,40,45,60)
y <- c(16,20,27,40,60)
plot(x, y)
Fig. 2. Histogram
2.3 Boxplot:
Boxplot provide a more terse summary than histograms, but they are easier to stack with other
boxplots. For example, here we can use them to compare the different regions:
murders$rate <- with(murders, total / population * 100000)
boxplot(rate~region, data = murders)
Fig. 3. BoxPlot
We can see that the South has higher murder rates than the other three regions.
2.4 Image:
The image function displays the values in a matrix using color. Here is a quick example:
x <- matrix(1:120, 12, 10)
image(x)
Fig.4. Image
2.5 Creating and Saving Graphs:
In a typical interactive session, you build a graph one statement at a time, adding features, until
you have what you want. Consider the following five lines:
attach(mtcars)
plot(wt, mpg)
abline(lm(mpg~wt))
title("Regression of MPG on Weight")
detach(mtcars)
The first statement attaches the data frame mtcars. The second statement opens a graphics
window and generates a scatter plot between automobile weight on the horizontal axis and
miles per gallon on the vertical axis. The third statement adds a line of best fit. The fourth
statement adds a title. The final statement detaches the data frame. In R, graphs are typically
created in this interactive fashion (see figure 5).
You can save your graphs via code or through GUI menus. To save a graph via code, sandwich
the statements that produce the graph between a statement that sets a destination and a
statement that closes that destination. For example, the following will save the graph as a PDF
document named mygraph.pdf in the current working directory:
>pdf("mygraph.pdf")
>attach(mtcars)
>plot(wt, mpg)
>abline(lm(mpg~wt))
>title("Regression of MPG on Weight")
>detach(mtcars)
>dev.off()
In addition to pdf(), you can use the functions win.metafile(), png(), jpeg(), bmp(), tiff(), xfig(),
and postscript() to save graphs in other formats. (Note: The Windows metafile format is only
available on Windows platforms.)
Saving graphs via the GUI is platform specific. On a Windows platform, select File >Save As
from the graphics window, and choose the format and location desired in the resulting dialog.
Creating a new graph by issuing a high-level plotting command such as plot(), hist() (for
histograms), or boxplot() typically overwrites a previous graph. How can you create more than
one graph and still have access to each? There are several methods.
First, you can open a new graph window before creating a new graph:
dev.new()
statements to create graph 1
dev.new()
statements to create a graph 2
etc.
Each new graph will appear in the most recently opened window.
Second, you can access multiple graphs via the GUI. On a Windows platform, you must use a
two-step process. After opening the first graph window, choose History > Recording. Then use
the Previous and Next menu items to step through the graphs that are created.
Finally, you can use the functions dev.new(), dev.next(), dev.prev(), dev.set(), and dev.off() to
have multiple graph windows open at one time and choose which output is sent to which
windows. This approach works on any platform. See help(dev.cur) for details on this approach.
2.6 Combining Graphs:
R makes it easy to combine several graphs into one overall graph, using either the par() or
layout() function.
With the par() function, you can include the graphical parameter mfrow=c(nrows, ncols) to
create a matrix of nrows × ncols plots that are filled in by row. Alternatively, you can use
mfcol=c(nrows, ncols) to fill the matrix by columns. For example, the following code creates
four plots and arranges them into two rows and two columns:
>attach(mtcars)
>opar <- par(no.readonly=TRUE)
>par(mfrow=c(2,2))
>plot(wt,mpg, main="Scatterplot of wt vs. mpg")
>plot(wt,disp, main="Scatterplot of wt vs. disp")
>hist(wt, main="Histogram of wt")
>boxplot(wt, main="Boxplot of wt")
>par(opar)
>detach(mtcars)
LAB SESSION
Lab Task:
1. To develop data visualisation in R programming.
Apparatus:
• Laptop
• R
Experimental Procedure:
1. How to Setup R:
1. Start R by double-click on the R icon on your desktop. It will open following windows
in your PC as shown in image.
EXPERIMENT DOMAIN:
Marks 3 5 3 3 3 1 2
distribution
LAB REPORT
Prepare the Lab Report as below:
TITLE:
OBJECTIVE:
APPARATUS:
PROCEDURE:
(Note: Use all steps you studied in LAB SESSION of this tab to write procedure and to
complete the experiment)
DISCUSSION:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
Q2.: List the data plotting function name that are commonly used in R?
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
_______________
Conclusion /Summary
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________