0% found this document useful (0 votes)
16 views40 pages

R Data Visualization Techniques

The document provides a comprehensive tutorial on data visualization in R, focusing on creating strip charts, plots, and saving these visualizations in various formats. It covers functions like stripchart(), plot(), jpeg(), png(), and pdf() for generating and saving plots, as well as techniques for adding titles, labels, and colors to enhance visual appeal. Additionally, it explains the use of color names, hexadecimal values, and RGB values to customize plot colors.

Uploaded by

vk3573373
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)
16 views40 pages

R Data Visualization Techniques

The document provides a comprehensive tutorial on data visualization in R, focusing on creating strip charts, plots, and saving these visualizations in various formats. It covers functions like stripchart(), plot(), jpeg(), png(), and pdf() for generating and saving plots, as well as techniques for adding titles, labels, and colors to enhance visual appeal. Additionally, it explains the use of color names, hexadecimal values, and RGB values to customize plot colors.

Uploaded by

vk3573373
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

SIP-I on

R Programming
By
Dr. Devanshu Tiwari
Assistant Professor
Centre for Computer Science and Business Management
MITS-DU, Gwalior
12/15/2024
Day 7 contents
R Data Visualization
• R Strip Chart
• R Plot Function
• R Save Plot
• Colors in R

12/15/2024
R Data Visualization
R Strip Chart
A strip chart is a type of chart that displays numerical data along
a single strip.
A strip chart can be used to visualize dozens of time series at
once.
Dataset to Create Strip Chart
In R, first we need to load the dataset of which we want to create
the strip chart of.
In this tutorial, we will be using the built-in dataset
named airquality to create a strip chart.
Let's see the first six rows of the dataset we will be using,
12/15/2024
R Data Visualization
R Strip Chart

12/15/2024
R Data Visualization
R Strip Chart
Create Strip Chart in R
In R, we use the stripchart() function to create a strip chart. For example,

In the above example, we have used the stripchart() function


and the $ operator to create a strip chart of the Ozone reading
of the airquality dataset. We can pass additional parameters to
control the way our plot looks.

12/15/2024
R Data Visualization
R Strip Chart

Add Title, Label, New Color to a Strip Chart in R


We can add titles, provide labels for the axes, and change the color of the strip chart in R. For example,
# add title, label, new color to strip chart
stripchart(airquality$Ozone,
main="Mean ozone in parts per billion at Roosevelt Island",
xlab="Parts Per Billion",
ylab="Ozone",
col="orange")

12/15/2024
R Data Visualization
R Strip Chart
In the above figure, we can see that we have added a title, and a label to the x-axis and y-axis, and changed the color

of the strip.

Here,

● main - adds the title "Mean ozone in parts per billion at Roosevelt Island"

● xlab - adds the label "Parts Per Billion" for x-axis


● ylab - add the label "Ozone" for y-axis

● col = "Orange" - changes the color of strip to orange

12/15/2024
R Data Visualization
R Strip Chart
Jitter Plot in R
Jitter plot is a variant of the strip plot with a better view of overlapping data points. It is useful when there are
large clusters of data points.

We pass method = "Jitter" inside the stripchart() method to create a strip chart without overlapping of points.
For example,
stripchart(airquality$Ozone,
main="Mean ozone in parts per billion at Roosevelt Island",
xlab="Parts Per Billion",
ylab="Ozone",
col="orange",
method = "jitter")

12/15/2024
R Data Visualization
R Strip Chart
Jitter Plot in R
In the above example, we have used the method parameter inside stripchart() to create a jitter plot.

stripchart(airquality$Ozone,

...

method = "jitter")

Here, method = "jitter" specifies the coincident points are plotted like stacked or jitter and no points are
overlapped.

12/15/2024
R Data Visualization
R Strip Chart
Multiple Strip Charts in R

We can draw multiple strip charts in a single plot, by passing in a list of numeric vectors. For example,
# create list of ozone and solar radiation reading of airquality dataset
list1 <- list("Ozone" = airquality$Ozone, "Solar Radiations" =
airquality$Solar.R)

stripchart(list1,
main="Mean ozone in parts per billion at Roosevelt Island",
xlab="Parts Per Billion",
col= c("orange","brown"),
method = "jitter")

12/15/2024
R Data Visualization
R Strip Chart
In the above example, we have passed a list named list1 with two vectors: Ozone and Solar

Radiation of airquality dataset inside stripchart() to create multiple strips.

We have also provided two colors to represent two different strip charts

● "orange" - to represent Ozone readings

● "brown" - to represent Solar.R readings

12/15/2024
R Data Visualization
R Plot Chart
A plot() function is a generic function that is used to plot points in a graph.

Plot One Point in R


Generally the ordered pair (x,y) represents a point on a graph.

In R, we pass specific points for x-axis and y-axis respectively as a parameter for the plot()
function to create a plot. For example,

# create one point at (2,4)

plot(2, 4)

12/15/2024
R Data Visualization
R Plot Chart
In the above example, we have used the plot() function to plot one point on a graph.

plot(2, 4)

Here,

● 2 - specifies point on the x-axis


● 4 - specifies point on the y-axis

12/15/2024
R Data Visualization
R Plot Chart
Plot Multiple Points in R
We can also plot multiple points on a graph in R. For that we use the R Vectors. For example,

# create a vector x
x <- c(2, 4, 6, 8)

# create a vector y

y <- c(1, 3, 5, 7)

# plot multiple points


plot(x, y)

Output
12/15/2024
R Data Visualization
R Plot Chart
Plot Multiple Points in R
In the above example, we can plot multiple points on a graph using the plot() function and R vector.

plot(x, y)

Here, we have passed two vectors: x and y inside plot() to plot multiple points.

The first item of x and y i.e. 2 and 1 respectively plots 1st point on graph and second item of x and y

plots 2nd point on graph and so on.

Output

12/15/2024
R Data Visualization
R Plot Chart
Plot Sequence of Points in R
In R, we use the plot() function and the : operator to draw a sequence of points. For example,

# draw sequence of points

plot(1:5)

Output

In the above example, we have used the plot() and the : operator to draw a sequence of points.

The plots are drawn in (1, 1), (2, 2), (3, 3), (4, 4), (5, 5) order.

12/15/2024
R Data Visualization
R Plot Chart
Draw a Line in R
We pass the type parameter inside the plot() function to change the plot type. For example,

# draw a line

plot(1:5, type="l")

Output

In the above example, we have used the type parameter inside plot() to change the type of plot.

plot(1:5, type = "l")

Here, type = "l" draws a line to connect all the points.

12/15/2024
R Data Visualization
R Plot Chart
Different Plot Types in R
In R, we can change the type of plot using the type parameter inside the plot() function.

Here are some of the most commonly used types of plot we can use inside plot():

12/15/2024
R Data Visualization
R Plot Chart
Add Title and Label to a Plot in R

We can add titles, provide labels for the axes of the plot in R. For example,

plot(1:5,

main="Plot Sequence of Points",

xlab="x-axis",

ylab="y-axis")

12/15/2024
R Data Visualization
R Plot Chart
Add Title and Label to a Plot in R
In the above figure, we can see that we have added a title, a label to the x-axis and y-axis.

Here,

● main - adds the title "Plot Sequence of Points"

● xlab - adds the label "x-axis" for x-axis


● ylab - add the label "y-axis" for y-axis

12/15/2024
R Data Visualization
R Plot Chart
Plot Trigonometric Function in R
In R, we can also plot trigonometric functions.

Let's generate a sine wave plot,


# sequence vector of values from -pi to pi with 0.1 interval

x = seq(-pi,pi,0.1)

# respective sine value of x

y = sin(x)

# plot y against x
plot(x,y)

12/15/2024
R Data Visualization
R Plot Chart
Plot Trigonometric Function in R
In the above example, we have generated a sine wave plot.

We have used the seq() function to create the sequence vector x of values from -pi to pi with 0.1

interval. And assigned respective sine values of x to y.

Finally, we plotted y against x using plot().

12/15/2024
R Data Visualization
R Save Plot
All the graphs (bar plot, pie chart, histogram, etc.) we plot in R programming are displayed
on the screen by default.
We can save these plots as a file on disk with the help of built-in functions.
It is important to know that plots can be saved as bitmap images (raster) which are fixed

size or as vector images which are easily resizable.


We will use the temperature column of built-in dataset airquality to demonstrate how the

plots are saved in R.


To demonstrate how the plots are saved, we will create and save a histogram plot.

12/15/2024
R Data Visualization
R Save Plot
Save Plot as bitmap Image
Most of the images we come across like jpeg or png are bitmap images. They have a fixed resolution and are

pixelated when zoomed enough.

Functions in R that help us save plots in this format are jpeg() and png().

12/15/2024
R Data Visualization
R Save Plot
1. Save as jpeg Image

In R, to save a plot in jpeg format, we use the jpeg() function. For example,
# save histogram in jpeg format in current directory

jpeg(file="[Link]")

# a histogram we want to save

hist(airquality$Temp) In the above example, we have used the jpeg()


function to save a histogram in our current
# call this function to save the file directory.
[Link]() ● "[Link]" - name of the histogram
we will save in our directory.
● hist(airquality$Temp) - a histogram we
want to save
12/15/2024 ● [Link]() - function call to save the file.
R Data Visualization
R Save Plot
2. Save as png Image

We use the png() function in R to save plot in png format. For example,
# save as png image in specific directory with 600*350 resolution

png(file="C:/Programiz/R-tutorial/[Link]",

width=600, height=350)

# a histogram we want to save


In the above example, we have used the png()
hist(airquality$Temp)
function to save a histogram in png format.
# a function call to save the file
Here, we have also specified the full path of the file
[Link]()
we want to save. We have also specified the width

12/15/2024 and height of the image to 600 and 350 respectively.


R Data Visualization
R Save Plot
Save Plot as Vector Image
We can save our plots as vector images in pdf and postscript formats using the pdg() and

postscript() function respectively.

The beauty of vector images is that it is easily resizable. Zooming on the image will not

compromise its quality.

12/15/2024
R Data Visualization
R Save Plot
Save as pdf Image

In R, to save a plot in pdf format, we use the pdf() function. For example,

# save histogram in pdf format in current directory


pdf(file="[Link]")

# a histogram we want to save


hist(airquality$Temp)

# call this function to save the file


[Link]()

Here, we have used the pdf() function to save the histogram in our current directory.
Using pdf() saves the plot in a high quality format.
12/15/2024
R Data Visualization
R Color
We can visually improve our plots by coloring them. This is generally done with the col

graphical parameter.

We can specify the name of the color we want as a string. For example, if we want our
plot to be a red color, we pass col = "red".

12/15/2024
R Data Visualization
R Color
Add Color to Plot in R
We use the following temp vector to create a barplot throughout this section.
# create a vector named temp
temp <- c(5,7,6,4,8)
# barplot of temp without coloring
barplot(temp, main="By default")
# barplot of temp with coloring
barplot(temp, col="coral", main="With coloring")

Here, we have passed col =


"coral" inside the barplot() function
to color our barplot with coral color. Output
Try replacing it with "green", "blue",
"violet", etc. and look at the
difference.

12/15/2024
R Data Visualization
R Color
Using Color Names to Change Plot Color in R

R programming has names for 657 colors. We can take a look at them all with the colors()
function,

# display all color names

colors()

Here, the colors() function returns a vector of all the color names in alphabetical order with the first element
being "white".

We can color our plot by indexing this vector. For example, col=colors()[655] is the same as col="yellow3".

12/15/2024
R Data Visualization
R Color
Using Hex Values as Colors in R
In R, instead of using a color name, color can also be defined with a hexadecimal
value.
We define a color as a 6 hexadecimal digit number of the form #RRGGBB. Where the RR
is for red, GG for green and BB for blue and value ranges from 00 to FF.
For example, #FF0000 would be red and #00FF00 would be green similarly, #FFFFFF
would be white and #000000 would be black.
Let's take a look at how to implement hex values as colors in R,

12/15/2024
R Data Visualization
R Color
Using Hex Values as Colors in R
# create a vector named temp

temp <- c(5,7,6,4,8)

# using hex value #c00000

barplot(temp, col="#c00000", main="#c00000")

# using hex value #AE4371

barplot(temp, col="#AE4371", main="#AE4371")

In the above example, we have passed the hex value for the col parameter inside the barplot() function.

Here,

● #c00000 - this hex is composed of 75.3% red, 0% green and 0% blue


● #AE4371 - this hex is composed of 68.24% red, 26.27% green and 44.31% blue
12/15/2024
R Data Visualization
R Color
Using RGB Values to Color Plot in R
The rgb() function in R allows us to specify red, green and blue components with a number
between 0 and 1. This function returns the corresponding hex code discussed above. For
example,
rgb(0, 1, 0) # prints "#00FF00"

rgb(0.3, 0.7, 0.9) # prints "#4DB3E6"

We can directly pass rgb() to the col parameter as:


# create a vector named temp

temp <- c(5,7,6,4,8)

# using rgb() to color barplot

barplot(temp, col = rgb(0.3, 0.7, 0.9), main="Using RGB Values")


12/15/2024
R Data Visualization
R Color
Color Cycling in R
We can color each bar of the barplot with a different color by providing a vector of colors.

If the number of colors provided is less than the number of bars, the color vector is recycled. For example,
# create a vector named temp

temp <- c(5,7,6,4,8)

# color with 5 different colors

barplot(temp, col=c("red", "coral", "blue", "yellow", "pink"), main="With 5


Colors")

# color with 3 different color, last two bars will be recycled

barplot(temp, col=c("red", "coral", "blue"), main="With 3 Color")

12/15/2024
R Data Visualization
R Color
Color Cycling in R

In the above example, at first we colored each bar of the barplot by providing a vector with 5
colors for 5 different bars.
For the second barplot, we have provided a vector with 3 different colors, so the color is recycled
for the last 2 bars.

12/15/2024
R Data Visualization
R Color
Using Color Palette in R
R programming offers 4 built in color palettes which can be used to quickly generate color vectors
of desired length.
They are: rainbow(), [Link](), [Link](), and [Link](). We pass in the
number of colors that we want.
Let's take a look at the example,
# use rainbow() to generate color palette

rainbow(5)

# Output: "#FF0000FF" "#CCFF00FF" "#00FF66FF" "#0066FFFF" "#CC00FFFF"

Here, notice that the hexadecimal numbers are 8 digits long. The last two digits are the
transparency level with FF being opaque and 00 being fully transparent.

12/15/2024
R Data Visualization
R Color
Using Color Palette in R
Example: Using Color Palette in R
# create a vector named temp

temp <- c(5,7,6,4,8)

# using rainbow()

barplot(temp, col=rainbow(5), main="rainbow")

# using [Link]()

barplot(temp, col=[Link](5), main="[Link]")

# using [Link]()

barplot(temp, col=[Link](5), main="[Link]")

# using [Link]()

barplot(temp, col=[Link](5), main="[Link]")


12/15/2024
R Data Visualization
R Color
Using Color Palette in R
Example: Using Color Palette in R

Here, we have used 4 built in color palettes which can be used to quickly generate color vectors of
desired length.

12/15/2024
R Data Visualization

Thank You

12/15/2024

You might also like