SlideShare a Scribd company logo
www.r-squared.in/git-hub
dataCrunch Data Visualization With R
Learn To Add Text Annotations To Plots
dataCrunch
Text Annotations
Slide 2
dataCrunchText Annotations: Objectives
Slide 3
In this section, we will learn to:
Add text annotations to the plots using
● text() function
● mtext() function
dataCrunchText Annotations: Introduction
Slide 4
The text() and the mtext() functions allow the user to add text annotations to the plots. While the
text() function places the text inside the plot, the mtext() function places the text on the margins of
the plot.
Below is the syntax for both the functions:
# the text function
text(x, y = NULL, labels = seq_along(x), adj = NULL,
pos = NULL, offset = 0.5, vfont = NULL,
cex = 1, col = NULL, font = NULL, ...)
# the mtext function
mtext(text, side = 3, line = 0, outer = FALSE, at = NA,
adj = NA, padj = NA, cex = NA, col = NA, font = NA, ...)
Let us explore each function and its arguments one by one:
dataCrunchText Annotations: text()
Slide 5
To add text annotations using the text() function, the following 3 arguments must be supplied:
● x: x axis coordinate
● y: y axis coordinate
● text: the text to be added to the plot
Below is a simple example:
# create a basic plot
plot(mtcars$disp, mtcars$mpg)
# add text
text(340, 30, "Sample Text")
The text appears at the coordinates (340, 30) on the
plot. Ensure that the text is enclosed in double quotes
and the coordinates provided are within the range of
the X and Y axis variable.
dataCrunchtext(): Color
Slide 6
# create a basic plot
plot(mtcars$disp, mtcars$mpg)
# modify the color of the text
text(340, 30, "Sample Text", col = "red")
Description
The color of the text can be modified using the col
argument in the text() function.
Code
dataCrunchtext(): Color
Slide 7
The below plot depicts the appearance of the text when different options for col are applied:
dataCrunchtext(): Font
Slide 8
# create a basic plot
plot(mtcars$disp, mtcars$mpg)
# modify the font of the text
text(340, 30, "Sample Text", font = 2)
Description
The font of the text can be modified using the font
argument in the text() function.
Code
dataCrunchtext(): Font
Slide 9
The below plot depicts the appearance of the text when different options for font are applied:
dataCrunchtext(): Font Family
Slide 10
# create a basic plot
plot(mtcars$disp, mtcars$mpg)
# modify the font family of the text
text(340, 30, "Sample Text", family = mono)
Description
The font family of the text can be modified using the
family argument in the text() function.
Code
dataCrunchtext(): Font Family
Slide 11
The below plot depicts the appearance of the text when different options for font family are applied:
dataCrunchtext(): Font Size
Slide 12
# create a basic plot
plot(mtcars$disp, mtcars$mpg)
# modify the size of the text
text(340, 30, "Sample Text", cex = 1.5)
Description
The size of the text can be modified using the cex
argument in the text() function.
Code
dataCrunchtext(): Font Size
Slide 13
The below plot depicts the appearance of the text when different options for font size are applied:
dataCrunchmtext(): Introduction
Slide 14
The mtext() function places text annotations on the margins of the plot instead of placing them
inside the plot. It allows the user to modify the location of the text in multiple ways and we will
explore them one by one.
Below is a simple example:
# create a basic plot
plot(mtcars$disp, mtcars$mpg)
# add text
mtext("Sample Text")
As you can see, the text is placed on
the margin of the plot and not inside
the plot. Next, we will learn to
specify the margin where the text
should be placed.
dataCrunchmtext(): Margin
Slide 15
# create a basic plot
plot(mtcars$disp, mtcars$mpg)
# specify the margin on which the text should appear
mtext("Sample Text", side = 1)
Description
The margin on which we want to place the
text can be specified using the side
argument. It takes 4 values from 1-4 each
representing one side of the plot.
Code
dataCrunchmtext(): Margin Options
Slide 16
The side argument can be used to specify the margin on which the text should be placed.
side Margin
1 Bottom
2 Left
3 Top
4 Right
dataCrunchmtext(): Margin
Slide 17
The below plot depicts the appearance of the text when different options for side are applied:
dataCrunchmtext(): Line
Slide 18
# create a basic plot
plot(mtcars$disp, mtcars$mpg)
# place the text away from the margin
mtext("Sample Text", line = 1)
Description
The line argument places the text at the
specified distance from the margin. The default
value is 0 and as the value increases the text
is placed farther from the margin and outside
the plot, and as the value decreases the text is
placed inside the plot and farther from the
margin.
Code
dataCrunchmtext(): Line
Slide 19
# create a basic plot
plot(mtcars$disp, mtcars$mpg)
# place the text away from the plot
mtext("Sample Text", line = -1)
Description
The line argument places the text inside the
plot when the values is less than zero.
Code
dataCrunchmtext(): Line
Slide 20
The below plot depicts the appearance of the text when different options for line are applied:
dataCrunchmtext(): adj
Slide 21
# create a basic plot
plot(mtcars$disp, mtcars$mpg)
# align the text to the left
mtext("Sample Text", adj= 0)
Description
The adj argument is used for horizontal
alignment of the text. If set to 0, the text will be
left aligned and at 1, it will be right aligned.
Code
dataCrunchmtext(): adj
Slide 22
# create a basic plot
plot(mtcars$disp, mtcars$mpg)
# align the text to the right
mtext("Sample Text", adj= 1)
Description
When the value is set to 1, the text will be right
aligned.
Code
dataCrunchmtext(): adj
Slide 23
The below plot depicts the appearance of the text when different options for text() are applied:
dataCrunch
Slide 24
Visit dataCrunch for
tutorials on:
→ R Programming
→ Business Analytics
→ Data Visualization
→ Web Applications
→ Package Development
→ Git & GitHub
Ad

Recommended

Data Visualization With R: Learn To Modify Font Of Graphical Parameters
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
Rsquared Academy
 
Data Visualization With R: Learn To Modify Color Of Plots
Data Visualization With R: Learn To Modify Color Of Plots
Rsquared Academy
 
R Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar Plots
Rsquared Academy
 
Data Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple Graphs
Rsquared Academy
 
Data Visualization With R: Introduction
Data Visualization With R: Introduction
Rsquared Academy
 
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
Rsquared Academy
 
Data Visualization With R
Data Visualization With R
Rsquared Academy
 
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
The Statistical and Applied Mathematical Sciences Institute
 
Access intermediate 2010 final project new
Access intermediate 2010 final project new
clscott1
 
Chapter13 two-dimensional-array
Chapter13 two-dimensional-array
Deepak Singh
 
An introduction to matlab
An introduction to matlab
Dr. Andrew Wallace PhD
 
R Programming: Numeric Functions In R
R Programming: Numeric Functions In R
Rsquared Academy
 
Extreme querying with_analytics
Extreme querying with_analytics
Gary Myers
 
Lecture 1 mte 407
Lecture 1 mte 407
rumanatasnim415
 
Lecture 1 mte 407
Lecture 1 mte 407
rumanatasnim415
 
R studio
R studio
Kinza Irshad
 
Excel/R
Excel/R
Andrija Djurovic
 
Introduction to matlab
Introduction to matlab
Sourabh Bhattacharya
 
R-Excel Integration
R-Excel Integration
Andrija Djurovic
 
11)recap forms and queries
11)recap forms and queries
panks172
 
statistical computation using R- an intro..
statistical computation using R- an intro..
Kamarudheen KV
 
Assigment 3
Assigment 3
sukumaraser
 
Doc 20180130-wa0004
Doc 20180130-wa0004
HarithaRanasinghe
 
Easy Blogging With Emacs -- Cheatsheet
Easy Blogging With Emacs -- Cheatsheet
Dashamir Hoxha
 
Matlab Visualizing Data
Matlab Visualizing Data
DataminingTools Inc
 
QMC: Undergraduate Workshop, Tutorial on 'R' Software - Yawen Guan, Feb 26, 2...
QMC: Undergraduate Workshop, Tutorial on 'R' Software - Yawen Guan, Feb 26, 2...
The Statistical and Applied Mathematical Sciences Institute
 
R graphics
R graphics
DHIVYADEVAKI
 
Tools for research plotting
Tools for research plotting
Nimrita Koul
 
Tools for research plotting
Tools for research plotting
Nimrita Koul
 
Presentation: Plotting Systems in R
Presentation: Plotting Systems in R
Ilya Zhbannikov
 

More Related Content

What's hot (18)

Access intermediate 2010 final project new
Access intermediate 2010 final project new
clscott1
 
Chapter13 two-dimensional-array
Chapter13 two-dimensional-array
Deepak Singh
 
An introduction to matlab
An introduction to matlab
Dr. Andrew Wallace PhD
 
R Programming: Numeric Functions In R
R Programming: Numeric Functions In R
Rsquared Academy
 
Extreme querying with_analytics
Extreme querying with_analytics
Gary Myers
 
Lecture 1 mte 407
Lecture 1 mte 407
rumanatasnim415
 
Lecture 1 mte 407
Lecture 1 mte 407
rumanatasnim415
 
R studio
R studio
Kinza Irshad
 
Excel/R
Excel/R
Andrija Djurovic
 
Introduction to matlab
Introduction to matlab
Sourabh Bhattacharya
 
R-Excel Integration
R-Excel Integration
Andrija Djurovic
 
11)recap forms and queries
11)recap forms and queries
panks172
 
statistical computation using R- an intro..
statistical computation using R- an intro..
Kamarudheen KV
 
Assigment 3
Assigment 3
sukumaraser
 
Doc 20180130-wa0004
Doc 20180130-wa0004
HarithaRanasinghe
 
Easy Blogging With Emacs -- Cheatsheet
Easy Blogging With Emacs -- Cheatsheet
Dashamir Hoxha
 
Matlab Visualizing Data
Matlab Visualizing Data
DataminingTools Inc
 
QMC: Undergraduate Workshop, Tutorial on 'R' Software - Yawen Guan, Feb 26, 2...
QMC: Undergraduate Workshop, Tutorial on 'R' Software - Yawen Guan, Feb 26, 2...
The Statistical and Applied Mathematical Sciences Institute
 

Similar to R Data Visualization: Learn To Add Text Annotations To Plots (20)

R graphics
R graphics
DHIVYADEVAKI
 
Tools for research plotting
Tools for research plotting
Nimrita Koul
 
Tools for research plotting
Tools for research plotting
Nimrita Koul
 
Presentation: Plotting Systems in R
Presentation: Plotting Systems in R
Ilya Zhbannikov
 
Introduction to R Short course Fall 2016
Introduction to R Short course Fall 2016
Spencer Fox
 
matplotlib.pptxdsfdsfdsfdsdsfdsdfdsfsdf cvvf
matplotlib.pptxdsfdsfdsfdsdsfdsdfdsfsdf cvvf
zmulani8
 
UNIT_4_data visualization.pptx
UNIT_4_data visualization.pptx
BhagyasriPatel2
 
M4_DAR_part1. module part 4 analystics with r
M4_DAR_part1. module part 4 analystics with r
LalithauLali
 
Matlab ch1 (6)
Matlab ch1 (6)
mohsinggg
 
UNit-III. part 2.pdf
UNit-III. part 2.pdf
MastiCreation
 
Lectures r-graphics
Lectures r-graphics
etyca
 
MatplotLib.pptx
MatplotLib.pptx
Paras Intotech
 
a9bf73_Introduction to Matplotlib01.pptx
a9bf73_Introduction to Matplotlib01.pptx
Rahidkhan10
 
UNIT-5-II IT-DATA VISUALIZATION TECHNIQUES
UNIT-5-II IT-DATA VISUALIZATION TECHNIQUES
hemalathab24
 
ggplot2: An Extensible Platform for Publication-quality Graphics
ggplot2: An Extensible Platform for Publication-quality Graphics
Claus Wilke
 
Rtips123
Rtips123
Mahendra Babu
 
Introduction to GGVIS Visualization
Introduction to GGVIS Visualization
HemantSingh311
 
Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2
izahn
 
Visualization and Matplotlib using Python.pptx
Visualization and Matplotlib using Python.pptx
SharmilaMore5
 
R programming for data science
R programming for data science
Sovello Hildebrand
 
Tools for research plotting
Tools for research plotting
Nimrita Koul
 
Tools for research plotting
Tools for research plotting
Nimrita Koul
 
Presentation: Plotting Systems in R
Presentation: Plotting Systems in R
Ilya Zhbannikov
 
Introduction to R Short course Fall 2016
Introduction to R Short course Fall 2016
Spencer Fox
 
matplotlib.pptxdsfdsfdsfdsdsfdsdfdsfsdf cvvf
matplotlib.pptxdsfdsfdsfdsdsfdsdfdsfsdf cvvf
zmulani8
 
UNIT_4_data visualization.pptx
UNIT_4_data visualization.pptx
BhagyasriPatel2
 
M4_DAR_part1. module part 4 analystics with r
M4_DAR_part1. module part 4 analystics with r
LalithauLali
 
Matlab ch1 (6)
Matlab ch1 (6)
mohsinggg
 
UNit-III. part 2.pdf
UNit-III. part 2.pdf
MastiCreation
 
Lectures r-graphics
Lectures r-graphics
etyca
 
a9bf73_Introduction to Matplotlib01.pptx
a9bf73_Introduction to Matplotlib01.pptx
Rahidkhan10
 
UNIT-5-II IT-DATA VISUALIZATION TECHNIQUES
UNIT-5-II IT-DATA VISUALIZATION TECHNIQUES
hemalathab24
 
ggplot2: An Extensible Platform for Publication-quality Graphics
ggplot2: An Extensible Platform for Publication-quality Graphics
Claus Wilke
 
Introduction to GGVIS Visualization
Introduction to GGVIS Visualization
HemantSingh311
 
Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2
izahn
 
Visualization and Matplotlib using Python.pptx
Visualization and Matplotlib using Python.pptx
SharmilaMore5
 
R programming for data science
R programming for data science
Sovello Hildebrand
 
Ad

More from Rsquared Academy (20)

Handling Date & Time in R
Handling Date & Time in R
Rsquared Academy
 
Market Basket Analysis in R
Market Basket Analysis in R
Rsquared Academy
 
Practical Introduction to Web scraping using R
Practical Introduction to Web scraping using R
Rsquared Academy
 
Joining Data with dplyr
Joining Data with dplyr
Rsquared Academy
 
Explore Data using dplyr
Explore Data using dplyr
Rsquared Academy
 
Data Wrangling with dplyr
Data Wrangling with dplyr
Rsquared Academy
 
Writing Readable Code with Pipes
Writing Readable Code with Pipes
Rsquared Academy
 
Introduction to tibbles
Introduction to tibbles
Rsquared Academy
 
Read data from Excel spreadsheets into R
Read data from Excel spreadsheets into R
Rsquared Academy
 
Read/Import data from flat/delimited files into R
Read/Import data from flat/delimited files into R
Rsquared Academy
 
Variables & Data Types in R
Variables & Data Types in R
Rsquared Academy
 
How to install & update R packages?
How to install & update R packages?
Rsquared Academy
 
How to get help in R?
How to get help in R?
Rsquared Academy
 
Introduction to R
Introduction to R
Rsquared Academy
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For Beginners
Rsquared Academy
 
R Markdown Tutorial For Beginners
R Markdown Tutorial For Beginners
Rsquared Academy
 
R Programming: Introduction to Matrices
R Programming: Introduction to Matrices
Rsquared Academy
 
R Programming: Introduction to Vectors
R Programming: Introduction to Vectors
Rsquared Academy
 
R Programming: Variables & Data Types
R Programming: Variables & Data Types
Rsquared Academy
 
R Programming: Mathematical Functions In R
R Programming: Mathematical Functions In R
Rsquared Academy
 
Market Basket Analysis in R
Market Basket Analysis in R
Rsquared Academy
 
Practical Introduction to Web scraping using R
Practical Introduction to Web scraping using R
Rsquared Academy
 
Writing Readable Code with Pipes
Writing Readable Code with Pipes
Rsquared Academy
 
Read data from Excel spreadsheets into R
Read data from Excel spreadsheets into R
Rsquared Academy
 
Read/Import data from flat/delimited files into R
Read/Import data from flat/delimited files into R
Rsquared Academy
 
Variables & Data Types in R
Variables & Data Types in R
Rsquared Academy
 
How to install & update R packages?
How to install & update R packages?
Rsquared Academy
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For Beginners
Rsquared Academy
 
R Markdown Tutorial For Beginners
R Markdown Tutorial For Beginners
Rsquared Academy
 
R Programming: Introduction to Matrices
R Programming: Introduction to Matrices
Rsquared Academy
 
R Programming: Introduction to Vectors
R Programming: Introduction to Vectors
Rsquared Academy
 
R Programming: Variables & Data Types
R Programming: Variables & Data Types
Rsquared Academy
 
R Programming: Mathematical Functions In R
R Programming: Mathematical Functions In R
Rsquared Academy
 
Ad

Recently uploaded (20)

Measurecamp Copenhagen - Consent Context
Measurecamp Copenhagen - Consent Context
Human37
 
UPS and Big Data intro to Business Analytics.pptx
UPS and Big Data intro to Business Analytics.pptx
sanjum5582
 
最新版美国佐治亚大学毕业证(UGA毕业证书)原版定制
最新版美国佐治亚大学毕业证(UGA毕业证书)原版定制
Taqyea
 
最新版美国约翰霍普金斯大学毕业证(JHU毕业证书)原版定制
最新版美国约翰霍普金斯大学毕业证(JHU毕业证书)原版定制
Taqyea
 
PPT1_CB_VII_CS_Ch3_FunctionsandChartsinCalc.ppsx
PPT1_CB_VII_CS_Ch3_FunctionsandChartsinCalc.ppsx
animaroy81
 
25 items quiz for practical research 1 in grade 11
25 items quiz for practical research 1 in grade 11
leamaydayaganon81
 
最新版美国威斯康星大学河城分校毕业证(UWRF毕业证书)原版定制
最新版美国威斯康星大学河城分校毕业证(UWRF毕业证书)原版定制
taqyea
 
Indigo dyeing Presentation (2).pptx as dye
Indigo dyeing Presentation (2).pptx as dye
shreeroop1335
 
Indigo_Airlines_Strategy_Presentation.pptx
Indigo_Airlines_Strategy_Presentation.pptx
mukeshpurohit991
 
Residential Zone 4 for industrial village
Residential Zone 4 for industrial village
MdYasinArafat13
 
Communication_Skills_Class10_Visual.pptx
Communication_Skills_Class10_Visual.pptx
namanrastogi70555
 
Attendance Presentation Project Excel.pptx
Attendance Presentation Project Excel.pptx
s2025266191
 
Camuflaje Tipos Características Militar 2025.ppt
Camuflaje Tipos Características Militar 2025.ppt
e58650738
 
定制OCAD学生卡加拿大安大略艺术与设计大学成绩单范本,OCAD成绩单复刻
定制OCAD学生卡加拿大安大略艺术与设计大学成绩单范本,OCAD成绩单复刻
taqyed
 
Allotted-MBBS-Student-list-batch-2021.pdf
Allotted-MBBS-Student-list-batch-2021.pdf
subhansaifi0603
 
Boost Business Efficiency with Professional Data Entry Services
Boost Business Efficiency with Professional Data Entry Services
eloiacs eloiacs
 
11_L2_Defects_and_Trouble_Shooting_2014[1].pdf
11_L2_Defects_and_Trouble_Shooting_2014[1].pdf
gun3awan88
 
PPT2 W1L2.pptx.........................................
PPT2 W1L2.pptx.........................................
palicteronalyn26
 
@Reset-Password.pptx presentakh;kenvtion
@Reset-Password.pptx presentakh;kenvtion
MarkLariosa1
 
Daily, Weekly, Monthly Report MTC March 2025.pptx
Daily, Weekly, Monthly Report MTC March 2025.pptx
PanjiDewaPamungkas1
 
Measurecamp Copenhagen - Consent Context
Measurecamp Copenhagen - Consent Context
Human37
 
UPS and Big Data intro to Business Analytics.pptx
UPS and Big Data intro to Business Analytics.pptx
sanjum5582
 
最新版美国佐治亚大学毕业证(UGA毕业证书)原版定制
最新版美国佐治亚大学毕业证(UGA毕业证书)原版定制
Taqyea
 
最新版美国约翰霍普金斯大学毕业证(JHU毕业证书)原版定制
最新版美国约翰霍普金斯大学毕业证(JHU毕业证书)原版定制
Taqyea
 
PPT1_CB_VII_CS_Ch3_FunctionsandChartsinCalc.ppsx
PPT1_CB_VII_CS_Ch3_FunctionsandChartsinCalc.ppsx
animaroy81
 
25 items quiz for practical research 1 in grade 11
25 items quiz for practical research 1 in grade 11
leamaydayaganon81
 
最新版美国威斯康星大学河城分校毕业证(UWRF毕业证书)原版定制
最新版美国威斯康星大学河城分校毕业证(UWRF毕业证书)原版定制
taqyea
 
Indigo dyeing Presentation (2).pptx as dye
Indigo dyeing Presentation (2).pptx as dye
shreeroop1335
 
Indigo_Airlines_Strategy_Presentation.pptx
Indigo_Airlines_Strategy_Presentation.pptx
mukeshpurohit991
 
Residential Zone 4 for industrial village
Residential Zone 4 for industrial village
MdYasinArafat13
 
Communication_Skills_Class10_Visual.pptx
Communication_Skills_Class10_Visual.pptx
namanrastogi70555
 
Attendance Presentation Project Excel.pptx
Attendance Presentation Project Excel.pptx
s2025266191
 
Camuflaje Tipos Características Militar 2025.ppt
Camuflaje Tipos Características Militar 2025.ppt
e58650738
 
定制OCAD学生卡加拿大安大略艺术与设计大学成绩单范本,OCAD成绩单复刻
定制OCAD学生卡加拿大安大略艺术与设计大学成绩单范本,OCAD成绩单复刻
taqyed
 
Allotted-MBBS-Student-list-batch-2021.pdf
Allotted-MBBS-Student-list-batch-2021.pdf
subhansaifi0603
 
Boost Business Efficiency with Professional Data Entry Services
Boost Business Efficiency with Professional Data Entry Services
eloiacs eloiacs
 
11_L2_Defects_and_Trouble_Shooting_2014[1].pdf
11_L2_Defects_and_Trouble_Shooting_2014[1].pdf
gun3awan88
 
PPT2 W1L2.pptx.........................................
PPT2 W1L2.pptx.........................................
palicteronalyn26
 
@Reset-Password.pptx presentakh;kenvtion
@Reset-Password.pptx presentakh;kenvtion
MarkLariosa1
 
Daily, Weekly, Monthly Report MTC March 2025.pptx
Daily, Weekly, Monthly Report MTC March 2025.pptx
PanjiDewaPamungkas1
 

R Data Visualization: Learn To Add Text Annotations To Plots

  • 1. www.r-squared.in/git-hub dataCrunch Data Visualization With R Learn To Add Text Annotations To Plots
  • 3. dataCrunchText Annotations: Objectives Slide 3 In this section, we will learn to: Add text annotations to the plots using ● text() function ● mtext() function
  • 4. dataCrunchText Annotations: Introduction Slide 4 The text() and the mtext() functions allow the user to add text annotations to the plots. While the text() function places the text inside the plot, the mtext() function places the text on the margins of the plot. Below is the syntax for both the functions: # the text function text(x, y = NULL, labels = seq_along(x), adj = NULL, pos = NULL, offset = 0.5, vfont = NULL, cex = 1, col = NULL, font = NULL, ...) # the mtext function mtext(text, side = 3, line = 0, outer = FALSE, at = NA, adj = NA, padj = NA, cex = NA, col = NA, font = NA, ...) Let us explore each function and its arguments one by one:
  • 5. dataCrunchText Annotations: text() Slide 5 To add text annotations using the text() function, the following 3 arguments must be supplied: ● x: x axis coordinate ● y: y axis coordinate ● text: the text to be added to the plot Below is a simple example: # create a basic plot plot(mtcars$disp, mtcars$mpg) # add text text(340, 30, "Sample Text") The text appears at the coordinates (340, 30) on the plot. Ensure that the text is enclosed in double quotes and the coordinates provided are within the range of the X and Y axis variable.
  • 6. dataCrunchtext(): Color Slide 6 # create a basic plot plot(mtcars$disp, mtcars$mpg) # modify the color of the text text(340, 30, "Sample Text", col = "red") Description The color of the text can be modified using the col argument in the text() function. Code
  • 7. dataCrunchtext(): Color Slide 7 The below plot depicts the appearance of the text when different options for col are applied:
  • 8. dataCrunchtext(): Font Slide 8 # create a basic plot plot(mtcars$disp, mtcars$mpg) # modify the font of the text text(340, 30, "Sample Text", font = 2) Description The font of the text can be modified using the font argument in the text() function. Code
  • 9. dataCrunchtext(): Font Slide 9 The below plot depicts the appearance of the text when different options for font are applied:
  • 10. dataCrunchtext(): Font Family Slide 10 # create a basic plot plot(mtcars$disp, mtcars$mpg) # modify the font family of the text text(340, 30, "Sample Text", family = mono) Description The font family of the text can be modified using the family argument in the text() function. Code
  • 11. dataCrunchtext(): Font Family Slide 11 The below plot depicts the appearance of the text when different options for font family are applied:
  • 12. dataCrunchtext(): Font Size Slide 12 # create a basic plot plot(mtcars$disp, mtcars$mpg) # modify the size of the text text(340, 30, "Sample Text", cex = 1.5) Description The size of the text can be modified using the cex argument in the text() function. Code
  • 13. dataCrunchtext(): Font Size Slide 13 The below plot depicts the appearance of the text when different options for font size are applied:
  • 14. dataCrunchmtext(): Introduction Slide 14 The mtext() function places text annotations on the margins of the plot instead of placing them inside the plot. It allows the user to modify the location of the text in multiple ways and we will explore them one by one. Below is a simple example: # create a basic plot plot(mtcars$disp, mtcars$mpg) # add text mtext("Sample Text") As you can see, the text is placed on the margin of the plot and not inside the plot. Next, we will learn to specify the margin where the text should be placed.
  • 15. dataCrunchmtext(): Margin Slide 15 # create a basic plot plot(mtcars$disp, mtcars$mpg) # specify the margin on which the text should appear mtext("Sample Text", side = 1) Description The margin on which we want to place the text can be specified using the side argument. It takes 4 values from 1-4 each representing one side of the plot. Code
  • 16. dataCrunchmtext(): Margin Options Slide 16 The side argument can be used to specify the margin on which the text should be placed. side Margin 1 Bottom 2 Left 3 Top 4 Right
  • 17. dataCrunchmtext(): Margin Slide 17 The below plot depicts the appearance of the text when different options for side are applied:
  • 18. dataCrunchmtext(): Line Slide 18 # create a basic plot plot(mtcars$disp, mtcars$mpg) # place the text away from the margin mtext("Sample Text", line = 1) Description The line argument places the text at the specified distance from the margin. The default value is 0 and as the value increases the text is placed farther from the margin and outside the plot, and as the value decreases the text is placed inside the plot and farther from the margin. Code
  • 19. dataCrunchmtext(): Line Slide 19 # create a basic plot plot(mtcars$disp, mtcars$mpg) # place the text away from the plot mtext("Sample Text", line = -1) Description The line argument places the text inside the plot when the values is less than zero. Code
  • 20. dataCrunchmtext(): Line Slide 20 The below plot depicts the appearance of the text when different options for line are applied:
  • 21. dataCrunchmtext(): adj Slide 21 # create a basic plot plot(mtcars$disp, mtcars$mpg) # align the text to the left mtext("Sample Text", adj= 0) Description The adj argument is used for horizontal alignment of the text. If set to 0, the text will be left aligned and at 1, it will be right aligned. Code
  • 22. dataCrunchmtext(): adj Slide 22 # create a basic plot plot(mtcars$disp, mtcars$mpg) # align the text to the right mtext("Sample Text", adj= 1) Description When the value is set to 1, the text will be right aligned. Code
  • 23. dataCrunchmtext(): adj Slide 23 The below plot depicts the appearance of the text when different options for text() are applied:
  • 24. dataCrunch Slide 24 Visit dataCrunch for tutorials on: → R Programming → Business Analytics → Data Visualization → Web Applications → Package Development → Git & GitHub