0% found this document useful (0 votes)
1 views

Unit 5 r Programming

This document provides an overview of advanced graphics in R, focusing on base-R graphics, high-level and low-level plotting functions, and graphical parameters. It covers essential plotting commands, customization options, and techniques for displaying multivariate data. Additionally, it discusses the use of mathematical annotations and Hershey vector fonts for enhanced text rendering in plots.

Uploaded by

divyashree
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Unit 5 r Programming

This document provides an overview of advanced graphics in R, focusing on base-R graphics, high-level and low-level plotting functions, and graphical parameters. It covers essential plotting commands, customization options, and techniques for displaying multivariate data. Additionally, it discusses the use of mathematical annotations and Hershey vector fonts for enhanced text rendering in plots.

Uploaded by

divyashree
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 43

ADVANCED GRAPHICS IN R

This section introduces the base-R graphics. Reading


the materials will familiarize you with different options
and commands used for plotting. You should start
coding by implementing the high-level function like the
plot, then incrementally modify and add code to
change the plot appearance and add the function par to
fine-tune the margins, etc. You will also learn about the
R graphics devices used to save plots for publications
(do not use the point-and-click interface to save plots
from RStudio); these device commands are also
applicable to outputs of the ggplot2.
High-level plotting commands
High-level plotting functions are designed to generate a
complete plot of the data passed as arguments to the
function. Where appropriate, axes, labels, and titles are
automatically generated (unless you request
otherwise.) High-level plotting commands always start
a new plot, erasing the current plot if necessary.
 The plot() function
 Displaying multivariate data
 Display graphics
 Arguments to high-level plotting functions

The plot() function


One of the most frequently used plotting functions in R
is the plot() function. This is a generic function: the
type of plot produced is dependent on the type
or class of the first argument.
plot(x, y)
plot(xy)
If x and y are vectors, plot(x, y) produces a
scatterplot of y against x. The same effect can be
produced by supplying one argument (second
form) as either a list containing two
elements x and y or a two-column matrix.
plot(x)

If x is a time series, this produces a time-series


plot. If x is a numeric vector, it produces a plot of
the values in the vector against their index in the
vector. If x is a complex vector, it produces a plot
of imaginary versus real parts of the vector
elements.
plot(f)
plot(f, y)

f is a factor object, y is a numeric vector. The first


form generates a bar plot of f; the second form
produces boxplots of y for each level of f.
plot(df)
plot(~ expr)
plot(y ~ expr)

df is a data frame, y is any object, expr is a list of


object names separated by '+' (e.g., a + b + c).
The first two forms produce distributional plots of
the variables in a data frame (first form) or of a
number of named objects (second form). The third
form plots y against every object named in expr.

Displaying multivariate data


R provides two very useful functions for representing
multivariate data. If X is a numeric matrix or data
frame, the command
> pairs(X)
produces a pairwise scatterplot matrix of the variables
defined by the columns of X, that is, every column
of X is plotted against every other column of X and the
resulting n(n-1) plots are arranged in a matrix with plot
scales constant over the rows and columns of the
matrix.
When three or four variables are involved a coplot may
be more enlightening. If a and b are numeric vectors
and c is a numeric vector or factor object (all of the
same length), then the command
> coplot(a ~ b | c)
produces a number of scatterplots of a against b for
given values of c. If c is a factor, this simply means
that a is plotted against b for every level of c. When c is
numeric, it is divided into a number of conditioning
intervals, and for each interval a is plotted against b for
values of c within the interval. The number and position
of intervals can be controlled
with given.values= argument to coplot() - the
function co.intervals() is useful for selecting intervals.
You can also use two given variables with a command
like
> coplot(a ~ b | c + d)
which produces scatterplots of a against b for every
joint conditioning interval of c and d.
The coplot() and pairs() function both take an
argument panel= which can be used to customize the
plot type appearing in each panel. The default
is points() to produce a scatterplot but by supplying
some other low-level graphics function of two
vectors x and y as the value of panel= you can produce
any type of plot you wish. An example panel function
useful for coplots is panel.smooth().

Display graphics
Other high-level graphics functions produce different
types of plots. Some examples are:
qqnorm(x)
qqline(x)
qqplot(x, y)

Distribution-comparison plots. The first form plots


the numeric vector x against the expected Normal
order scores (a normal scores plot), and the
second adds a straight line to such a plot by
drawing a line through the distribution and data
quartiles. The third form plots the quantiles
of x against those of y to compare their respective
distributions.
hist(x)
hist(x, nclass=n)
hist(x, breaks=b, …)

Produces a histogram of the numeric vector x. A


sensible number of classes is usually chosen, but a
recommendation can be given with
the nclass= argument. Alternatively, the
breakpoints can be specified exactly with
the breaks= argument. If
the probability=TRUE argument is given, the bars
represent relative frequencies divided by bin width
instead of counts.
dotchart(x, …)
Constructs a dotchart of the data in x. In a
dotchart the y-axis gives a labeling of the data
in x , and the x-axis gives its value. For example, it
allows easy visual selection of all data entries with
values lying in specified ranges.
image(x, y, z, …)
contour(x, y, z, …)
persp(x, y, z, …)

Plots of three variables. The image plot draws a


grid of rectangles using different colors to
represent the value of z, the contour plot draws
contour lines to represent the value of z, and
the persp plot draws a 3D surface.

Arguments to high-level plotting functions


There are a number of arguments that may be passed
to high-level graphics functions, as follows:
add=TRUE

Forces the function to act as a low-level graphics


function, superimposing the plot on the current
plot (some functions only).
axes=FALSE

Suppresses generation of axes - useful for adding


your own custom axes with the axis() function. The
default, axes=TRUE, means include axes.
log="x"
log="y"
log="xy"

Causes the x, y or both axes to be logarithmic.


This will work for many, but not all, types of plot.
type=
The type= argument controls the type of plot
produced, as follows:
type="p"
Plot individual points (the default)
type="l"
Plot lines
type="b"
Plot points connected by lines (both)
type="o"
Plot points overlaid by lines
type="h"
Plot vertical lines from points to the zero axis
(high-density)
type="s"
type="S"
Step-function plots. In the first form, the top of the
vertical defines the point; in the second, the
bottom.
type="n"
No plotting at all. However, axes are still drawn
(by default), and the coordinate system is set up
according to the data. Ideal for creating plots with
subsequent low-level graphics functions.
xlab=string
ylab=string
Axis labels for the x and y axes. Use these
arguments to change the default labels, usually
the names of the objects used in the call to the
high-level plotting function.
main=string
Figure title, placed at the top of the plot in a large
font.
sub=string
Sub-title is placed just below the x-axis in a
smaller font.

Low-level plotting commands


Sometimes the high-level plotting functions don't
produce exactly the kind of plot you desire. In this case,
low-level plotting commands can be used to add extra
information (such as points, lines or text) to the current
plot.
Some of the more useful low-level plotting functions
are:
points(x, y)
lines(x, y)
Adds points or connected lines to the current
plot. plot()'s type= argument can also be passed
to these functions (and defaults
to "p" for points() and "l" for lines().)
text(x, y, labels, …)
Add text to a plot at points given by x, y.
Normally labels is an integer or character vector,
in which case labels[i] is plotted at point (x[i], y[i]).
The default is 1:length(x).
Note: This function is often used in the sequence
> plot(x, y, type="n"); text(x, y, names)
The graphics parameter type="n" suppresses the
points but sets up the axes, and the text() function
supplies special characters, as specified by the
character vector names for the points.
abline(a, b)
abline(h=y)
abline(v=x)
abline(lm.obj)

Adds a line of slope b and intercept a to the


current plot. h=y may be used to specify y-
coordinates for the heights of horizontal lines to go
across a plot, and v=x similarly for the x-
coordinates for vertical lines. Also lm.obj may be
list with a coefficients component of length 2 (such
as the result of model-fitting functions,) which are
taken as an intercept and slope, in that order.
polygon(x, y, …)

Draws a polygon defined by the ordered vertices


in (x, y) and (optionally) shade it in with hatch
lines or fill it if the graphics device allows the filling
of figures.
legend(x, y, legend, …)

Adds a legend to the current plot at the specified


position. Plotting characters, line styles, colors,
etc., are identified with the labels in the character
vector legend. At least one other argument v (a
vector the same length as legend) with the
corresponding values of the plotting unit must also
be given as follows:
legend( , fill=v)
Colors for filled boxes
legend( , col=v)
Colors in which points or lines will be drawn
legend( , lty=v)
Line styles
legend( , lwd=v)
Line widths
legend( , pch=v)
Plotting characters (character vector)
title(main, sub)

Adds a title main to the top of the current plot in a


large font and (optionally) a sub-title sub at the
bottom in a smaller font.

axis(side, …)

Adds an axis to the current plot on the side given


by the first argument (1 to 4, counting clockwise
from the bottom.) Other arguments control the
positioning of the axis within or beside the plot,
and tick positions and labels. Useful for adding
custom axes after calling plot() with
the axes=FALSE argument.
Low-level plotting functions usually require some
positioning information (e.g., x and y coordinates) to
determine where to place the new plot elements.
Coordinates are given in terms of user
coordinates which are defined by the previous high-
level graphics command and are chosen based on the
supplied data.
Where x and y arguments are required, it is also
sufficient to supply a single argument being a list with
elements named x and y. Similarly, a matrix with two
columns is also valid input. In this way, functions such
as locator() (see below) may be used to specify
positions on a plot interactively.
 Mathematical annotation
 Hershey vector fonts

Mathematical annotation
In some cases, it is useful to add mathematical symbols
and formulae to a plot. This can be achieved in R by
specifying an expression rather than a character string
in any one of text, mtext, axis, or title. For example, the
following code draws the formula for the Binomial
probability function:
> text(x, y, expression(paste(bgroup("(", atop(n, x),
")"), p^x, q^{n-x})))
More information, including a full listing of the features
available, can be obtained from within R using the
commands:
> help(plotmath)
> example(plotmath)
> demo(plotmath)

Hershey vector fonts


It is possible to specify Hershey vector fonts for
rendering text when using
the text and contour functions. There are three reasons
for using the Hershey fonts:
 Hershey fonts can produce better output,
especially on a computer screen, for rotated
and/or small text.
 Hershey fonts provide certain symbols that
may not be available in the standard fonts. In
particular, there are zodiac signs, cartographic
symbols, and astronomical symbols.
 Hershey fonts provide cyrillic and japanese
(Kana and Kanji) characters.
More information, including tables of Hershey
characters, can be obtained from within R using the
commands:
> help(Hershey)
> demo(Hershey)
> help(Japanese)
> demo(Japanese)

Graphics parameter list


This section introduces the base-R graphics. Reading
the materials will familiarize you with different options
and commands used for plotting. You should start
coding by implementing the high-level function like the
plot, then incrementally modify and add code to
change the plot appearance and add the function par to
fine-tune the margins, etc. You will also learn about the
R graphics devices used to save plots for publications
(do not use the point-and-click interface to save plots
from RStudio); these device commands are also
applicable to outputs of the ggplot2.
Graphics parameters list
The following sections detail many of the commonly-
used graphical parameters. The R help documentation
for the par() function provides a more concise
summary; this is provided as a somewhat more
detailed alternative.
Graphics parameters will be presented in the following
form:
name=value
A description of the parameter's effect. name is
the name of the parameter, that is, the argument
name to use in calls to par() or a graphics
function. value is a typical value you might use
when setting the parameter.
Note that axes is not a graphics parameter but an
argument to a few plot methods: see xaxt and yaxt.
 Graphical elements
 Axes and tick marks
 Figure margins
 Multiple figure environment

Graphical elements
R plots are made up of points, lines, text, and polygons
(filled regions.) Graphical parameters exist that control
how these graphical elements are drawn, as follows:
pch="+"
Character to be used for plotting points. The
default varies with graphics drivers, but it is
usually a circle. Plotted points tend to appear
slightly above or below the appropriate position
unless you use "." as the plotting character, which
produces centered points.
pch=4
When pch is given as an integer between 0 and 25
inclusive, a specialized plotting symbol is
produced. To see what the symbols are, use the
command
> legend(locator(1), as.character(0:25), pch =
0:25)
Those from 21 to 25 may appear to duplicate
earlier symbols but can be colored in different
ways: see the help on points and its examples.
In addition, pch can be a character or a number in
the range 32:255 representing a character in the
current font.
lty=2
Line types. Alternative line styles are not
supported on all graphics devices (and vary on
those that do), but line type 1 is always a solid
line, line type 0 is always invisible, and line types 2
and onwards are dotted or dashed lines or some
combination of both.
lwd=2
Line widths. Desired width of lines, in multiples of
the “standard" line width. Affects axis lines as well
as lines drawn with lines(), etc. Not all devices
support this, and some have restrictions on the
widths that can be used.
col=2
Colors to be used for points, lines, text, filled
regions, and images. A number from the current
palette (see ?palette) or a named color.
col.axis
col.lab
col.main
col.sub
The color to be used for axis
annotation, x and y labels, main and sub-titles,
respectively.
font=2
An integer that specifies which font to use for text.
If possible, device drivers should be arranged to
correspond to plain text, 2 to boldface, 3 to
italic, 4 to bold italic, and 5 to a symbol font
(which includes Greek letters).
font.axis
font.lab
font.main
font.sub
The font to be used for axis
annotation, x and y labels, main and sub-titles,
respectively.
adj=-0.1
Justification of text relative to the plotting
position. 0 means left justify, 1 means right justify
and 0.5 means to center horizontally about the
plotting position. The actual value is the proportion
of text that appears to the left of the plotting
position, so a value of -0.1 leaves a gap of 10% of
the text width between the text and the plotting
position.
cex=1.5
Character expansion. The value is the desired size
of text characters (including plotting characters)
relative to the default text size.
cex.axis
cex.lab
cex.main
cex.sub
The character expansion to be used for axis
annotation, x and y labels, and main and sub-
titles, respectively.

Axes and tick marks


Many of R's high-level plots have axes, and you can
construct axes yourself with the low-
level axis() graphics function. Axes have three main
components: the axis line (line style controlled by
the lty graphics parameter), the tick marks (which mark
off unit divisions along the axis line) and the tick
labels (which mark the units.) These components can
be customized with the following graphics parameters.
lab=c(5, 7, 12)
The first two numbers are the desired number of
tick intervals on the x and y axes respectively. The
third number is the desired length of axis labels, in
characters (including the decimal point.) Choosing
a too-small value for this parameter may result in
all tick labels being rounded to the same number!
las=1
Orientation of axis labels. 0 means always parallel
to axis, 1 means always horizontal, and 2 means
always perpendicular to the axis.
mgp=c(3, 1, 0)
Positions of axis components. The first component
is the distance from the axis label to the axis
position in text lines. The second component is the
distance to the tick labels, and the final
component is the distance from the axis position
to the axis line (usually zero). Positive numbers
measure outside the plot region, and negative
numbers inside.
tck=0.01
Length of tick marks as a fraction of the size of the
plotting region. When tck is small (less than 0.5),
the tick marks on the x and y axes are forced to be
the same size. A value of 1 gives grid lines.
Negative values give tick marks outside the
plotting region. Use tck=0.01 and mgp=c(1,-
1.5,0) for internal tick marks.
xaxs="r"
yaxs="i"
Axis styles for the x and y axes, respectively. With
styles "i" (internal) and "r" (the default) tick marks
always fall within the range of the data, however
the style "r" leaves a small amount of space at the
edges

Figure margins
A single plot in R is known as a figure and comprises
a plot region surrounded by margins (possibly
containing axis labels, titles, etc.) and (usually)
bounded by the axes themselves.
A typical figure is
Graphics parameters controlling figure layout include:
mai=c(1, 0.5, 0.5, 0)
Widths of the bottom, left, top and right margins,
respectively, are measured in inches.
mar=c(4, 2, 2, 1)
Similar to mai, except the measurement unit is
text lines.
mar and mai are equivalent in the sense that setting
one changes the value of the other. The default values
chosen for this parameter are often too large; the right-
hand margin is rarely needed, and neither is the top
margin if no title is being used. The bottom and left
margins must be large enough to accommodate the
axis and tick labels. Furthermore, the default is chosen
without regard to the size of the device surface: for
example, using the postscript() driver with
the height=4 argument will result in a plot that is about
50% margin unless mar or mai are set explicitly. When
multiple figures are in use (see below), the margins are
reduced. However, this may not be enough when many
figures share the same page.

Multiple figure environment


R allows you to create an n by m array of figures on a
single page. Each figure has its own margins, and the
array of figures is optionally surrounded by an outer
margin, as shown in the following figure.

The graphical parameters relating to multiple figures


are as follows:
mfcol=c(3, 2)
mfrow=c(2, 4)
Set the size of a multiple figure array. The first
value is the number of rows; the second is the
number of columns. The only difference between
these two parameters is that setting mfcol causes
figures to be filled by column; mfrow fills by rows.
The layout in the Figure could have been created
by setting mfrow=c(3,2); the figure shows the
page after four plots have been drawn.
Setting either of these can reduce the base size of
symbols and text (controlled by par("cex") and the
pointsize of the device). In a layout with exactly
two rows and columns, the base size is reduced by
a factor of 0.83: if there are three or more of rows
or columns, the reduction factor is 0.66.
mfg=c(2, 2, 3, 2)
Position of the current figure in a multiple figure
environment. The first two numbers are the row
and column of the current figure; the last two are
the number of rows and columns in the multiple
figure array. Set this parameter to jump between
figures in the array. You can even use different
values for the last two numbers than
the true values for unequally-sized figures on the
same page.
fig=c(4, 9, 1, 4)/10
Position of the current figure on the page. Values
are the positions of the left, right, bottom and top
edges, respectively, as a percentage of the page
measured from the bottom left corner. The
example value would be for a figure in the bottom
right of the page. Set this parameter for arbitrary
positioning of figures within a page. If you want to
add a figure to a current page, use new=TRUE as
well (unlike S).
oma=c(2, 0, 3, 0)
omi=c(0, 0, 0.8, 0)
Size of outer margins. Like mar and mai, the first
measures in text lines and the second in inches,
starting with the bottom margin and working
clockwise.
Outer margins are particularly useful for page-wise
titles, etc. Text can be added to the outer margins with
the mtext() function with argument outer=TRUE. There
are no outer margins by default, however, so you must
create them explicitly using oma or omi.
More complicated arrangements of multiple figures can
be produced by
the split.screen() and layout() functions, as well
Device Drivers
This section introduces the base-R graphics. Reading
the materials will familiarize you with different options
and commands used for plotting. You should start
coding by implementing the high-level function like the
plot, then incrementally modify and add code to
change the plot appearance and add the function par to
fine-tune the margins, etc. You will also learn about the
R graphics devices used to save plots for publications
(do not use the point-and-click interface to save plots
from RStudio); these device commands are also
applicable to outputs of the ggplot2.
Device drivers
R can generate graphics (of varying levels of quality) on
almost any type of display or printing device. Before
this can begin, however, R needs to be informed of
what type of device it is dealing with. This is done by
starting a device driver. The purpose of a device driver
is to convert graphical instructions from R ("draw a
line," for example) into a form that the particular device
can understand.
Device drivers are started by calling a device driver
function. There is one such function for every device
driver: type help(Devices) for a list of them all. For
example, issuing the command
> postscript()
causes all future graphics output to be sent to the
printer in PostScript format. Some commonly-used
device drivers are:
X11()
For use with the X11 window system on Unix-alikes
windows()
For use on Windows
quartz()
For use on macOS
postscript()
For printing on PostScript printers or creating PostScript
graphics files.
pdf()
Produces a PDF file, which can also be included into
PDF files.
png()
Produces a bitmap PNG file. (Not always available: see
its help page.)
jpeg()
Produces a bitmap JPEG file, best used for image plots.
(Not always available: see its help page.)
When you have finished with a device, be sure to
terminate the device driver by issuing the command
> dev.off()
This ensures that the device finishes cleanly; for
example, in the case of hardcopy devices, every page
is completed and sent to the printer. (This will happen
automatically at the normal end of a session.)
 PostScript diagrams for typeset documents

 Multiple graphics devices

PostScript diagrams for typeset documents


Bypassing the file argument to the postscript() device
driver function, you may store the graphics in
PostScript format in a file of your choice. The plot will
be in a landscape orientation unless
the horizontal=FALSE argument is given, and you can
control the size of the graphic with
the width and height arguments (the plot will be scaled
as appropriate to fit these dimensions.) For example,
the command
> postscript("file.ps", horizontal=FALSE, height=5,
pointsize=10)
will produce a file containing PostScript code for a
figure five inches high, perhaps for inclusion in a
document. It is important to note that if the file named
in the command already exists, it will be overwritten.
This is the case even if the file was only created earlier
in the same R session.
Many usages of PostScript output will be to incorporate
the figure in another document. This works best
when encapsulated PostScript is produced: R always
produces conformant output but only marks the output
as such when the onefile=FALSE argument is supplied.
This unusual notation stems from S-compatibility: it
really means that the output will be a single page
(which is part of the EPSF specification). Thus to
produce a plot for inclusion, use something like
> postscript("plot1.eps", horizontal=FALSE,
onefile=FALSE,
height=8, width=6, pointsize=10)

Multiple graphics devices


In advanced use of R it is often useful to have several
graphics devices in use at the same time. Of course,
only one graphics device can accept commands at any
time, which is known as the current device. When
multiple devices are open, they form a numbered
sequence with names giving the kind of device at any
position.
The main commands used for operating with multiple
devices and their meanings are as follows:
X11()
[UNIX]
windows()
win.printer()
win.metafile()
[Windows]
quartz()
[macOS]
postscript()
pdf()
png()
jpeg()
tiff()
bitmap()

Each new call to a device driver function opens a new
graphics device, thus extending by one the device list.
This device becomes the current device to which
graphics output will be sent.
dev.list()
Returns the number and name of all active devices. The
device at position 1 on the list is always the null
device that does not accept graphics commands.
dev.next()
dev.prev()
Returns the number and name of the graphics device
next to, or previous to the current device, respectively.
dev.set(which=k)
Can be used to change the current graphics device to
the one at position k of the device list. Returns the
number and label of the device.
dev.off(k)
Terminate the graphics device at point k of the device
list. For some devices, such as postscript devices, this
will either print the file immediately or correctly
complete the file for later printing, depending on how
the device was initiated.
dev.copy(device, …, which=k)
dev.print(device, …, which=k)
Make a copy of the device k. Here device is a device
function, such as postscript, with extra arguments, if
needed, specified by '…'. dev.print is similar, but the
copied device is immediately closed, so that end
actions, such as printing hardcopies, are immediately
performed.
graphics.off()
Terminate all graphics devices on the list except the n

Dynamic graphics in r programming


In R programming, dynamic graphics refer to
visualizations that can be updated or modified in real
time, often in response to user input or changing data.
These types of visualizations can be useful in
interactive applications or exploratory data analysis, as
they allow users to interact with the data and see
immediate changes in the graphical output.
There are several libraries in R that facilitate the
creation of dynamic graphics:
1. plotly – Interactive Plotting
plotly is one of the most popular libraries for creating
interactive plots. It integrates with ggplot2 and can be
used to create dynamic charts like scatter plots, line
graphs, bar charts, and more. With plotly, you can add
hover effects, zoom, pan, and update plots
interactively.
Example:
r
Copy code
# Install plotly if you haven't already
# install.packages("plotly")

library(plotly)

# Create a simple scatter plot


plot_ly(data = mtcars, x = ~mpg, y = ~hp, type =
'scatter', mode = 'markers')
This will create an interactive scatter plot where you
can zoom, pan, and hover over points for more
information.
2. shiny – Web Applications with Interactive
Graphics
shiny is a web application framework for R that makes
it easy to build interactive web apps. You can integrate
interactive plots created using plotly, ggplot2, or other
libraries within a shiny app to create dynamic, user-
driven visualizations.
Example of a simple shiny app with dynamic
graphics:
r
Copy code
# Install shiny if you haven't already
# install.packages("shiny")

library(shiny)
library(plotly)

ui <- fluidPage(
titlePanel("Dynamic Scatter Plot"),
sidebarLayout(
sidebarPanel(
sliderInput("slider", "Select x-axis range:",
min = 0, max = 100, value = c(0, 100))
),
mainPanel(
plotlyOutput("scatterPlot")
)
)
)

server <- function(input, output) {


output$scatterPlot <- renderPlotly({
# Filter mtcars data based on the slider input
filtered_data <- subset(mtcars, mpg >=
input$slider[1] & mpg <= input$slider[2])

# Create an interactive plot


plot_ly(filtered_data, x = ~mpg, y = ~hp, type =
'scatter', mode = 'markers')
})
}

shinyApp(ui, server)
This app will dynamically update the scatter plot based
on the slider input, allowing users to filter data by the
mpg variable.
3. ggplot2 with plotly – Combining Static and
Dynamic Graphics
ggplot2 is a widely used static plotting package in R,
but you can convert static ggplot2 plots into dynamic
interactive plots using the plotly library.
Example:
r
Copy code
# Install ggplot2 if you haven't already
# install.packages("ggplot2")

library(ggplot2)
library(plotly)

# Create a static ggplot2 plot


p <- ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point()

# Convert the static plot to an interactive plot


interactive_plot <- ggplotly(p)

# Display the interactive plot


interactive_plot
This will turn the static ggplot2 plot into an interactive
one with zooming, panning, and tooltips.
4. highcharter – Interactive Charts
The highcharter package is another tool for creating
interactive charts. It is an R wrapper for the Highcharts
JavaScript library, which is known for producing high-
quality interactive visualizations.
Example:
r
Copy code
# Install highcharter if you haven't already
# install.packages("highcharter")

library(highcharter)

# Create an interactive line chart


highchart() %>%
hc_chart(type = "line") %>%
hc_add_series(data = c(1, 2, 3, 4, 5), name =
"Series1") %>%
hc_title(text = "Interactive Line Chart")
This will create an interactive line chart that allows for
zooming and panning.
5. leaflet – Interactive Maps
For dynamic geographic maps, leaflet is a great library.
It enables the creation of interactive maps where users
can zoom, pan, and click on elements.
Example:
r
Copy code
# Install leaflet if you haven't already
# install.packages("leaflet")

library(leaflet)

# Create an interactive map


leaflet() %>%
addTiles() %>%
addMarkers(lng = -73.9857, lat = 40.7484, popup =
"Empire State Building")
This creates a map with a marker on the Empire State
Building. Users can interact with the map by zooming
and panning.
6. DT – Interactive Tables
The DT package is used to create interactive data
tables, which can be a useful way to explore data
dynamically. It provides features like sorting, searching,
and pagination.
Example:
r
Copy code
# Install DT if you haven't already
# install.packages("DT")

library(DT)

# Create an interactive data table


datatable(mtcars)
This will generate a table of the mtcars dataset, and
users can sort, filter, and paginate through the data
interactively.
plot customization in r

In R, you can customize your plots in many ways to


improve their appearance, functionality, and
readability. Below are some of the most common
customization options for creating both static and
interactive plots using different R packages like
ggplot2, base R graphics, and plotly.
1. Base R Plot Customization
Base R plots are created using the plot() function, and
you can customize many aspects of the plot, such as
the axis labels, titles, point types, colors, and more.
Example of Base R Plot Customization:
r
Copy code
# Create some example data
x <- 1:10
y <- x^2

# Base plot with customization


plot(x, y,
type = "b", # Both points and lines
col = "blue", # Color of points and lines
pch = 19, # Point type (filled circle)
lwd = 2, # Line width
xlab = "X-Axis Label", # X-axis label
ylab = "Y-Axis Label", # Y-axis label
main = "Customized Plot",# Main title
cex.main = 1.5, # Title size
cex.lab = 1.2, # Label size
col.main = "darkred", # Title color
col.lab = "darkgreen" # Label color
)
Customization options in Base R:
 type – Defines the type of plot, e.g., "p" (points), "l"
(lines), "b" (both).
 col – Color of points or lines.
 pch – Symbol for points.
 lwd – Line width.
 xlab and ylab – Axis labels.
 main – Main title of the plot.
 cex.main, cex.lab, etc. – Controls the size of text.
 col.main, col.lab – Title and label colors.
2. Customizing ggplot2 Plots
ggplot2 provides a high-level and flexible way to create
highly customizable plots. You can adjust themes,
labels, colors, scales, and more.
Example of ggplot2 Plot Customization:
r
Copy code
# Install ggplot2 if you haven't already
# install.packages("ggplot2")

library(ggplot2)
# Example data
df <- data.frame(x = 1:10, y = (1:10)^2)

# Basic ggplot2 plot with customization


ggplot(df, aes(x = x, y = y)) +
geom_point(color = "blue", size = 3) + #
Customizing points
geom_line(color = "red", size = 1.5) + # Customizing
line
labs(
title = "Customized ggplot2 Plot", # Title
x = "X-Axis", # X-axis label
y = "Y-Axis" # Y-axis label
)+
theme(
plot.title = element_text(size = 16, face = "bold",
color = "darkred"), # Title style
axis.title = element_text(size = 12, color =
"darkgreen"), # Axis labels style
axis.text = element_text(size = 10, color = "blue")
# Axis text style
)+
theme_minimal() # Use a minimal theme
Customization options in ggplot2:
 geom_point(), geom_line() – Add points and lines
with specific aesthetics like color, size, shape, and
more.
 labs() – Customize labels like the title and axis
labels.
 theme() – Adjust text size, face, color, and
background. You can customize the overall theme
of the plot with theme_*() functions.
 theme_minimal(), theme_bw(), theme_classic() –
Predefined themes for different plot styles.
3. Adding Custom Annotations in ggplot2
In ggplot2, you can add annotations like text labels,
arrows, and shapes to make your plot more
informative.
Example of Adding Annotations:
r
Copy code
ggplot(df, aes(x = x, y = y)) +
geom_point(color = "blue") +
geom_line(color = "red") +
annotate("text", x = 5, y = 25, label = "Point (5, 25)",
size = 4, color = "black") + # Text annotation
annotate("segment", x = 5, y = 25, xend = 7, yend =
50, color = "black", size = 1) + # Line annotation
labs(title = "Plot with Annotations")
Types of annotations in ggplot2:
 annotate("text", ...) – Adds text annotations.
 annotate("segment", ...) – Adds arrows or line
segments.
 annotate("rect", ...) – Adds rectangles.
4. Interactive Plot Customization with plotly
plotly enables interactive customization, where you can
add tooltips, hover effects, and dynamic behavior.
Example of Interactive Plot with Customization:
r
Copy code
# Install plotly if you haven't already
# install.packages("plotly")

library(plotly)

# Create a simple scatter plot


p <- plot_ly(data = mtcars, x = ~mpg, y = ~hp, type =
'scatter', mode = 'markers',
marker = list(color = 'rgb(0,0,255)', size = 12))

# Customize layout
p <- p %>% layout(
title = "Interactive Plot with Plotly",
xaxis = list(title = "Miles Per Gallon (mpg)"),
yaxis = list(title = "Horsepower (hp)"),
showlegend = FALSE
)

# Display plot
p
Customization options in plotly:
 marker – Customize the points, such as color, size,
and symbol.
 layout() – Customize titles, axis labels, and
legends.
 showlegend – Show or hide the legend.
 hoverinfo – Customize the information shown when
hovering over points.
5. Customizing highcharter Plots
The highcharter package allows the creation of
interactive plots, which can be customized with colors,
tooltips, and more.
Example of Customizing a highcharter Plot:
r
Copy code
# Install highcharter if you haven't already
# install.packages("highcharter")

library(highcharter)
# Simple line chart with customization
highchart() %>%
hc_chart(type = "line") %>%
hc_add_series(data = c(1, 2, 3, 4, 5), name =
"Series1") %>%
hc_title(text = "Custom Highcharter Plot") %>%
hc_subtitle(text = "An interactive line chart") %>%
hc_xAxis(title = list(text = "X-Axis")) %>%
hc_yAxis(title = list(text = "Y-Axis")) %>%
hc_tooltip(headerFormat =
"<b>{series.name}</b><br>", pointFormat = "X:
{point.x}, Y: {point.y}")
Customization options in highcharter:
 hc_title() and hc_subtitle() – Titles for the chart and
subtitle.
 hc_xAxis() and hc_yAxis() – Axis labels and other
settings.
 hc_tooltip() – Customizes the tooltip on hover.
6. Advanced Customizations:
For more advanced customizations, you can:
 Adjust Color Palettes: Use custom color
schemes with scale_color_manual(),
scale_fill_manual(), or RColorBrewer for ggplot2.
 Faceting: Use facet_wrap() or facet_grid() in
ggplot2 to create subplots based on categories.
 Themes: Use pre-built themes or create custom
themes in ggplot2 to standardize the appearance
of your plots.
plotting regions and margin in r
In R, plotting regions and margins can be
customized to adjust the layout and spacing of your
plots, making them more visually appealing or better
suited to the data you're visualizing. These
customizations are primarily handled through the par()
function in base R, while for more complex layouts or
multi-plot arrangements, the mfrow or mfcol arguments
in par() can be used. For ggplot2 and interactive plots,
adjustments can be made as well.
1. Plotting Regions and Margins in Base R
Plotting Regions:
The plotting region refers to the area where the plot is
drawn. In base R, you can use par(mfrow = c(n, m)) or
par(mfcol = c(n, m)) to divide the plotting area into
multiple regions (subplots).
 mfrow: Specifies the number of rows and columns
for the plotting layout.
 mfcol: Specifies the number of rows and columns,
but plots are filled by columns rather than rows.
Example: Multiple Plots in One Window
r
Copy code
# Set up a 2x2 plotting grid (2 rows, 2 columns)
par(mfrow = c(2, 2))

# Plot 1
plot(1:10, 1:10, main = "Plot 1")

# Plot 2
plot(1:10, (1:10)^2, main = "Plot 2")

# Plot 3
plot(1:10, sqrt(1:10), main = "Plot 3")

# Plot 4
plot(1:10, log(1:10), main = "Plot 4")

# Reset to a single plotting region


par(mfrow = c(1, 1))
Customization of Plot Regions:
 mfrow and mfcol control how the plotting window
is divided into subregions.
 You can mix different types of plots (e.g., scatter
plots, line charts) within each region.
Margins in Base R:
Margins define the space around the plot, including the
space for axis labels and titles. The margins can be
adjusted using the mar parameter inside par().
 The mar argument takes a numeric vector of
length 4 to specify the number of lines for each
margin:
o mar = c(bottom, left, top, right)
Example: Customizing Margins
r
Copy code
# Adjust margins to have more space on the bottom
and left
par(mar = c(5, 6, 3, 3)) # bottom=5, left=6, top=3,
right=3

# Create a plot with customized margins


plot(1:10, (1:10)^2, main = "Plot with Custom
Margins")

# Reset the margins to default


par(mar = c(5, 4, 4, 2))
Important Notes:
 mar = c(5, 4, 4, 2) is the default margin setting.
 If you need more space for axis labels or titles, you
can increase the margin values.
2. Customizing Margins for Single Plot
You can modify margins for a single plot to make the
labels and titles fit better or create more space
between different components.
Example of Customizing Margins for a Single
Plot:
r
Copy code
# Set larger margins for a single plot
par(mar = c(6, 6, 4, 4))

# Create a plot with large margins


plot(1:10, (1:10)^3, main = "Customized Margins")

# Reset to default margins


par(mar = c(5, 4, 4, 2))
Here, the mar = c(6, 6, 4, 4) creates more space on the
left and bottom of the plot, which is useful when you
have long axis labels or titles.
3. Using oma for Outer Margins in Base R
In addition to inner margins, you can also adjust the
outer margins of the plot area using the oma parameter
in the par() function.
 oma = c(bottom, left, top, right) sets the outer
margins.
Example: Adjusting Outer Margins
r
Copy code
# Adjust outer margins
par(oma = c(2, 2, 2, 2)) # Outer margins (bottom, left,
top, right)

# Set up a 2x2 grid of plots


par(mfrow = c(2, 2))

# Create four plots


plot(1:10, 1:10, main = "Plot 1")
plot(1:10, (1:10)^2, main = "Plot 2")
plot(1:10, sqrt(1:10), main = "Plot 3")
plot(1:10, log(1:10), main = "Plot 4")

# Reset outer margins


par(oma = c(0, 0, 0, 0))
Outer margins are typically used for adding space
around the entire layout of the plots, such as for labels,
legends, or titles that span across multiple plots.
4. Using layout() for More Complex Layouts
layout() offers more control over the plotting area by
allowing you to specify the exact position of each plot
in a multi-region layout.
Example: Using layout() to Customize Plot
Regions
r
Copy code
# Define layout matrix for 2 rows and 2 columns
layout(matrix(c(1, 2, 3, 3), nrow = 2, ncol = 2))

# Plot 1 (Top Left)


plot(1:10, 1:10, main = "Plot 1")

# Plot 2 (Top Right)


plot(1:10, (1:10)^2, main = "Plot 2")

# Plot 3 (Bottom Right, spanning two rows)


plot(1:10, log(1:10), main = "Plot 3")

# Reset layout to default


layout(1)
In this example:
 The layout matrix matrix(c(1, 2, 3, 3), nrow = 2,
ncol = 2) creates a custom layout where Plot 3
occupies two rows.
5. Margins and Plot Layout in ggplot2
In ggplot2, you don't directly control the margins like in
base R. However, you can adjust the spacing between
subplots using theme() to control the margins and
other spacing elements.
Example of Customizing Margins in ggplot2:
r
Copy code
# Load ggplot2
library(ggplot2)

# Create some example data


df <- data.frame(x = 1:10, y = (1:10)^2)

# Create a ggplot2 plot with custom theme for margins


ggplot(df, aes(x = x, y = y)) +
geom_point() +
labs(title = "ggplot2 Custom Margins") +
theme(
plot.margin = margin(2, 4, 2, 4, "cm") # Top, Right,
Bottom, Left margins in cm
)
In this example, plot.margin adjusts the space around
the plot in ggplot2. You can specify the margin values
in various units (e.g., cm, mm, inches, pt).
histogram, bar chart, pie chart, scatter plots in r
In R, there are several ways to create histograms, bar
charts, pie charts, and scatter plots. These can be
created using base R plotting functions, ggplot2 (a
popular package for data visualization), or other
specialized packages like plotly for interactive plots.
Below is a detailed guide on how to create each of
these plots in R.
1. Histogram
A histogram displays the distribution of a continuous
variable by grouping data into bins.
Base R Histogram:
r
Copy code
# Sample data
data <- rnorm(1000) # 1000 random normal numbers

# Base R histogram
hist(data,
main = "Histogram of Random Data", # Title
xlab = "Value", # X-axis label
col = "lightblue", # Bar color
border = "black", # Bar border color
breaks = 30, # Number of bins
freq = FALSE # Use density (TRUE
uses frequency counts)
)
ggplot2 Histogram:
r
Copy code
# Load ggplot2 package
library(ggplot2)

# Create data
data <- data.frame(value = rnorm(1000))
# ggplot2 histogram
ggplot(data, aes(x = value)) +
geom_histogram(binwidth = 0.5, fill = "skyblue", color
= "black", alpha = 0.7) +
labs(title = "ggplot2 Histogram", x = "Value", y =
"Frequency")
2. Bar Chart
A bar chart is typically used to represent categorical
data with rectangular bars.
Base R Bar Chart:
r
Copy code
# Sample data
categories <- c("A", "B", "C", "D")
values <- c(10, 15, 7, 25)

# Base R bar plot


barplot(values,
names.arg = categories, # Names for each bar
col = "orange", # Bar color
main = "Base R Bar Chart", # Title
xlab = "Category", # X-axis label
ylab = "Frequency" # Y-axis label
)
ggplot2 Bar Chart:
r
Copy code
# Sample data frame
data <- data.frame(category = c("A", "B", "C", "D"),
value = c(10, 15, 7, 25))

# ggplot2 bar chart


ggplot(data, aes(x = category, y = value)) +
geom_bar(stat = "identity", fill = "lightgreen", color =
"black") +
labs(title = "ggplot2 Bar Chart", x = "Category", y =
"Frequency")
3. Pie Chart
A pie chart is used to display the relative proportions
of a categorical variable.
Base R Pie Chart:
r
Copy code
# Sample data
sizes <- c(10, 15, 7, 25)
labels <- c("Category A", "Category B", "Category C",
"Category D")

# Base R pie chart


pie(sizes,
labels = labels,
main = "Pie Chart",
col = c("lightblue", "lightgreen", "lightcoral",
"lightskyblue")
)
ggplot2 Pie Chart:
To create a pie chart in ggplot2, we typically use the
geom_bar() function along with coord_polar() to convert
a bar chart into a circular pie chart.
r
Copy code
# Sample data frame
data <- data.frame(
category = c("A", "B", "C", "D"),
value = c(10, 15, 7, 25)
)

# ggplot2 pie chart


ggplot(data, aes(x = "", y = value, fill = category)) +
geom_bar(stat = "identity", width = 1) +
coord_polar(theta = "y") + # Converts the bar chart
to a pie chart
labs(title = "ggplot2 Pie Chart") +
theme_void() # Remove background grid lines and
axis
4. Scatter Plot
A scatter plot is used to visualize the relationship
between two continuous variables.
Base R Scatter Plot:
r
Copy code
# Sample data
x <- rnorm(100) # 100 random values
y <- rnorm(100) # 100 random values

# Base R scatter plot


plot(x, y,
main = "Scatter Plot", # Title
xlab = "X-axis", # X-axis label
ylab = "Y-axis", # Y-axis label
pch = 19, # Point type (solid circle)
col = "blue" # Point color
)
ggplot2 Scatter Plot:
r
Copy code
# Sample data
data <- data.frame(x = rnorm(100), y = rnorm(100))

# ggplot2 scatter plot


ggplot(data, aes(x = x, y = y)) +
geom_point(color = "darkblue", size = 2) +
labs(title = "ggplot2 Scatter Plot", x = "X-axis", y = "Y-
axis")
5. Interactive Scatter Plot with plotly
For an interactive scatter plot, we can use plotly, which
allows zooming, hovering, and more.
Plotly Interactive Scatter Plot:
r
Copy code
# Install plotly if not installed
# install.packages("plotly")

library(plotly)

# Sample data
x <- rnorm(100)
y <- rnorm(100)

# Create a plotly scatter plot


plot_ly(x = x, y = y, type = "scatter", mode =
"markers",
marker = list(color = "blue", size = 10)) %>%
layout(title = "Interactive Scatter Plot", xaxis =
list(title = "X-axis"), yaxis = list(title = "Y-axis"))

You might also like