0% found this document useful (0 votes)
11 views10 pages

Experiment # 4

The document outlines a lab experiment focused on developing data visualization using R programming, detailing objectives, procedures, and evaluation criteria. It includes instructions for setting up R, utilizing various plotting functions, and combining graphs. The evaluation sheet assesses students' knowledge, practical skills, and understanding of data visualization concepts.

Uploaded by

intoxop7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

Experiment # 4

The document outlines a lab experiment focused on developing data visualization using R programming, detailing objectives, procedures, and evaluation criteria. It includes instructions for setting up R, utilizing various plotting functions, and combining graphs. The evaluation sheet assesses students' knowledge, practical skills, and understanding of data visualization concepts.

Uploaded by

intoxop7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Lab Name: To develop data visualisation in R programming.

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.__________________________

Marking Evaluation Sheet

Knowledge components Domain Taxonomy Contribution Max. Obtained


level marks marks

1. Student is aware with


requirement and use of Imitation (P1) 3
apparatus involved in
experiment.
2. Student has conducted the Psychomotor 70%
experiment by practicing the Manipulate (P2) 11
hands-on skills as per
instructions.
3. Student has achieved required -
Precision (P3)
accuracy in performance.

4. Student is aware of discipline &


safety rules to follow them rules Receiving (A1) 2
Affective
during experiment.
20%

5. Student has responded well and


Respond (A2) 2
contributed affectively in
respective lab activity.
6. Student understands use of Understand.
modern programming languages Cognitive 10% 2
and software environment for (C2)
Data Mining (DM)
Total 20

Normalize
marks out of 5
(5)

Signed by Course teacher/ Lab Instructor


EXPERIMENT # 4
To develop data visualisation in R programming.

PRE LAB TASK

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. 1. Scatter plot


2.2 Histogram:
Histograms are a powerful graphical summary of a list of numbers that gives you a general
overview of the types of values you have. We can make a histogram of our murder rates by
simply typing:
x <- with(murders, total / population * 100000)
hist(x)

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).

Fig.5. Creating Graphs

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-up the Microsoft Windows.


2. Open the website http://cran.r-project.org or use Pin drive to access software folder
named R-3.6.2-win.exe
3. Double click on the software folder and double click on ‘R-3.6.2-win.exe’ file and run
the setup.
4. Press next until you reach the window which ask for the key.
5. Finally chose Finish and close the installation.

2. Get started with 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.

Fig. 1. R Startup GUI window

2. Install a package named dslabs


> install.packages("dslabs")
3. Load the example dataset stored in library named dslabs with name murders
> library(dslabs)
> data(murders)
4. Examine the dataset in detail for overview
>str(murders)
'data.frame': 51 obs. of 5 variables:
$ state : chr "Alabama" "Alaska" "Arizona" "Arkansas" ...
$ abb : chr "AL" "AK" "AZ" "AR" ...
$ region : Factor w/ 4 levels "Northeast","South",..: 2 4 4 2 4 4 1 2 2 2 ...
$ population: num 4779736 710231 6392017 2915918 37253956 ...
$ total : num 135 19 232 93 1257 ...
OR
>head(murders)
state abb region population total
1 Alabama AL South 4779736 135
2 Alaska AK West 710231 19
3 Arizona AZ West 6392017 232
4 Arkansas AR South 2915918 93
5 California CA West 37253956 1257
6 Colorado CO West 5029196 65
5. Make scatter plot of total murders vs population.
>x <- murders$population / 10^6
>y <- murders$total
>plot(x, y)
6. Make histogram of murders rate.
x <- with(murders, total / population * 100000)
hist(x)
7. Compare murders rate with region using boxplot.with Select the state with max number
of murders
>murders$rate <- with(murders, total / population * 100000)
>boxplot(rate~region, data = murders))
8. Make image using matrix of a hypothetical data:
>x <- matrix(1:120, 12, 10)
>image(x)
9. Combine graphs made above in step 5 to 8:

Extra Credit Points:


(Follow Similar procedure as well as using PRE-LAB TASK Session data complete the tasks
provided to you as Exercise)

EXPERIMENT DOMAIN:

Domains Psychomotor (70%) Affective (20%) Cognitive


(10%)
Attributes Realization of Conducting Data Data Discipline Individual Understa
Experiment Experiment Collection Analysis Participation nd
(Receiving)
(Awareness) (Act) (Use (Perform) (Respond/
Instrument) Contribute)
Taxonomy P1 P2 P2 P2 A1 A2 C2
Level

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:

Q1.: Why plotting is necessary for data scientists?

________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________

Q2.: List the data plotting function name that are commonly used in R?
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
_______________

Conclusion /Summary
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________

Domains Psychomotor (70%) Affective (20%) Cognitive


(10%)
Attributes Realization of Conducting Data Data Discipline Individual Understa
Experiment Experiment Collection Analysis Participation nd
(Receiving)
(Awareness) (Act) (Use (Perform) (Respond/
Instrument) Contribute)
Taxonomy P1 P2 P2 P2 A1 A2 C2
Level
Marks 3 5 3 3 2 2 2
distribution
Obtained
Marks

You might also like