Complete Download Visualizing Data in R 4 Graphics Using the base graphics stats and ggplot2 Packages 1st Edition Margot Tollefson PDF All Chapters
Complete Download Visualizing Data in R 4 Graphics Using the base graphics stats and ggplot2 Packages 1st Edition Margot Tollefson PDF All Chapters
com
https://textbookfull.com/product/visualizing-data-
in-r-4-graphics-using-the-base-graphics-stats-and-
ggplot2-packages-1st-edition-margot-tollefson/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/r-graphics-cookbook-practical-
recipes-for-visualizing-data-2nd-edition-winston-chang/
textboxfull.com
https://textbookfull.com/product/ggplot2-elegant-graphics-for-data-
analysis-second-edition-hadley-wickham/
textboxfull.com
https://textbookfull.com/product/ggplot2-elegant-graphics-for-data-
analysis-second-edition-hadley-wickham-carson-sievert/
textboxfull.com
https://textbookfull.com/product/using-r-and-rstudio-for-data-
management-statistical-analysis-and-graphics-2nd-edition-nicholas-j-
horton/
textboxfull.com
R in Action Data Analysis and Graphics with R Bonus ch 23
ONLY 2nd Edition Robert Kabacoff
https://textbookfull.com/product/r-in-action-data-analysis-and-
graphics-with-r-bonus-ch-23-only-2nd-edition-robert-kabacoff/
textboxfull.com
https://textbookfull.com/product/r-graphics-third-edition-paul-
murrell/
textboxfull.com
https://textbookfull.com/product/r-cookbook-proven-recipes-for-data-
analysis-statistics-and-graphics-jd-long/
textboxfull.com
https://textbookfull.com/product/fundamentals-of-graphics-using-
matlab-1st-edition-ranjan-parekh/
textboxfull.com
https://textbookfull.com/product/computer-graphics-programming-in-
opengl-using-c-third-edition-gordon-phd/
textboxfull.com
Visualizing
Data in R 4
Graphics Using the base, graphics,
stats, and ggplot2 Packages
—
Margot Tollefson
Visualizing Data in R 4
Graphics Using the base, graphics,
stats, and ggplot2 Packages
Margot Tollefson
Visualizing Data in R 4: Graphics Using the base, graphics, stats, and ggplot2
Packages
Margot Tollefson
Stratford, IA, USA
3.4 Details����������������������������������������������������������������������������������������������������������������������������������� 31
3.4.1 Colors��������������������������������������������������������������������������������������������������������������������������� 31
3.4.2 Fonts and Font Families����������������������������������������������������������������������������������������������� 38
3.4.3 Character Size in plot( )������������������������������������������������������������������������������������������������� 42
3.4.4 Line Details: lwd, lend, ljoin, and lmitre������������������������������������������������������������������������ 46
3.4.5 Making Changes to the Axes���������������������������������������������������������������������������������������� 49
3.4.6 Working with Log Scales���������������������������������������������������������������������������������������������� 58
vi
Table of Contents
vii
Table of Contents
viii
Table of Contents
Index��������������������������������������������������������������������������������������������������������������������� 395
ix
About the Author
Margot Tollefson, PhD, is a semiretired freelance statistician, with her own consulting
business, Vanward Statistics. She received her PhD in statistics from Iowa State
University and has many years of experience applying R to statistical research problems.
Dr. Tollefson has chosen to write this book because she often creates graphics using
R and would like to share her knowledge and experience. Her professional blog is on
WordPress at vanwardstat. She has social media accounts on LinkedIn, Facebook, and
Twitter. Her social media name is @vanstat on Twitter.
xi
About the Technical Reviewer
Tom Barker is an engineer and technology leader, a professor, and an author. He has
authored several books on web development, data visualization, and technical leadership,
including High Performance Responsive Design, Intelligent Caching, Pro JavaScript
Performance: Monitoring and Visualization, and Pro Data Visualization Using R and
JavaScript.
xiii
Acknowledgments
I would like to acknowledge the Comprehensive R Archive Network (CRAN). Without
the R documentation, for which the Comprehensive R Archive Network is responsible,
this book could not have been written. Also, my husband, Clay, for bearing with me
while I wrote.
xv
PART I
An Overview of plot()
CHAPTER 1
3
© Margot Tollefson 2021
M. Tollefson, Visualizing Data in R 4, https://doi.org/10.1007/978-1-4842-6831-5_1
Chapter 1 Introduction: plot( ), qplot( ), and ggplot( ), Plus Some
In Chapter 2, we describe the basics of the plot() function. In Chapter 3, the various
arguments to plot() are categorized. The ancillary functions for plot() are introduced
in Chapter 4. In Chapter 5, the methods (types of plots) for which plot() is defined are
presented. In Chapter 6, the possible arguments to the function par() are described,
and a way to set up multiple plots using par(), layout() or split.screen() is given. Also,
graphics devices are covered.
plotting functions are given in the appendixes at the end of this book, along with brief
descriptions of what the functions do and how to use them.
Appendix A lists functions used with contingency tables. Appendix B gives functions
for continuous variables. Appendix C lists functions that generate multiple plots.
Appendix D gives functions that smooth data. Appendix E gives plotting functions used
in time series analysis. Appendix F lists the plotting functions that are in the stats and
graphics packages and not covered in the first five appendixes.
1.4 S
oftware Versions and Hardware Used in
This Book
The versions of R used in this book are R 4.0.1 and R 4.0.3; the versions of RStudio are
1.3.595 and 1.3.1093. Since R and RStudio are constantly changing, the Comprehensive R
Archive Network (CRAN) provides news on changes to R. The news for R 4.0.1 and R 4.0.0
can be found at https://cran.r-project.org/doc/manuals/r-release/NEWS.pdf.
The beginning of that news pdf is shown in Figure 1-1.
5
Chapter 1 Introduction: plot( ), qplot( ), and ggplot( ), Plus Some
The computer used for the examples is a MacBook Air running macOS Catalina
version 10.15.5.
6
Chapter 1 Introduction: plot( ), qplot( ), and ggplot( ), Plus Some
7
CHAPTER 2
9
© Margot Tollefson 2021
M. Tollefson, Visualizing Data in R 4, https://doi.org/10.1007/978-1-4842-6831-5_2
Chapter 2 The plot( ) Function
2.3 Methods
The methods of a function are those classes of objects for which a function is defined.
In the graphics and stats packages, there are 29 methods defined for plot().
In Chapters 3 and 4, we usually use the version of plot() that takes an x, and possibly
a y, as the object(s) to be plotted and which plots a scatterplot of x against index values
or of y against x. The actual name of the function is plot.default().
However, since R automatically determines the method to use when running plot(),
the “.default” extension is not necessary in the call to plot(). For plot.default(), the x and y
would need to be equal-length vectors that can be coerced to numeric. The methods for
plot() are given in Chapter 5, along with what each method creates.
The par() function contains the default values that many of the arguments to plot()
and the ancillary functions use. The default arguments of par() can be changed – for a
given R session or within a function call – by a call to par(). Most of the arguments in
par() are the same as those arguments in plot() that affect the appearance of the graphic.
Some arguments in par() can only be set in par().
R allows multiple plots to be put in one graphic. A grid for multiple plots can be
created using one of two arguments of par(). The grid created by par() has the same
number of columns in each row. Alternatively, the function layout() can be used to create
a more flexible design, with differing numbers of columns in each row. The split.screen()
function allows for placing plots at different locations on a graphics device.
In Chapter 6, the types of graphics devices are listed, as well as ways to work with
graphics devices. More on par(), layout(), and split.screen() is also found in Chapter 6.
Figure 2-1. Plots of the time series sunspot.year (found in the datasets package
in R). The first plot uses the default argument settings in plot(). In the second plot,
some of the arguments are set.
11
Chapter 2 The plot( ) Function
The class of sunspot.year is ts, so the values in the time series are plotted against
time. The default axis labels are Time on the x axis and the name of the object that is
plotted on the y axis. By default, no title or subtitle is plotted.
For the preceding figure, the function par() is used to put two plots in a row into one
figure. After plot() is run twice, the number of plots per figure is changed back to one by
running par() again. Changes to par() remain in effect throughout an R session, unless
changed.
12
CHAPTER 3
data( "LifeCycleSavings" )
> force(LifeCycleSavings)
sr pop15 pop75 dpi ddpi
Australia 11.43 29.35 2.87 2329.68 2.87
Austria 12.07 23.32 4.41 1507.99 3.93
(Here, only the first two observations are displayed.) The dataset will also appear in
the Source (upper-left) window.
13
© Margot Tollefson 2021
M. Tollefson, Visualizing Data in R 4, https://doi.org/10.1007/978-1-4842-6831-5_3
Chapter 3 The Arguments of plot( )
Description
Usage
LifeCycleSavings
Format
Details
Source
The data were obtained from Belsley, Kuh and Welsch (1980). They
in turn obtained the data from Sterling (1977).
14
Chapter 3 The Arguments of plot( )
References
The five variables in the dataset are aggregate personal savings (sr), the percentage of
the population under 15 years of age (pop15), the percentage of the population over 75
years of age (pop75), real per-capita disposable income (dpi), and the percentage growth
rate of real per-capita disposable income (ddpi). The values of the variables are averages
taken over the years 1960–1970, and the averages were found for 50 countries.
For data frames, a variable in the data frame can be accessed by the data frame
name followed by a dollar sign followed by the variable name. For example,
LifeCycleSavings$pop75 accesses the pop75 variable in the LifeCycleSavings data
frame.
15
Chapter 3 The Arguments of plot( )
Figure 3-1. Default scatterplot of two variables from the LifeCycleSavings dataset
Note that the first argument is on the x axis (horizontal axis) and the second on the
y axis (vertical axis). Also, the graphic does not contain a title or subtitle, and the axis
labels contain the names of the variables in the dataset. The points are scattered so
that the most extreme points are close to the axes. In this chapter, we will show how to
change the appearance of, mainly, this plot by setting arguments.
16
Chapter 3 The Arguments of plot( )
Line breaks can be added to a character string by including “\n” in the character
string at the location of the line break. Or a character vector with more than one element
puts each element on a separate line (except with the argument sub).
The axis limits are the values from which the axis starts and at which the axis ends.
Having the start value greater than the end value is an accepted option. To set the x axis
and y axis limits of the plot, we use the arguments xlim and ylim. The format for the two
arguments is a numeric vector of length two, where the first number is the beginning
limit and the second number is the ending limit. By default, points outside the limits do
not plot.
An example of setting the labels and axis arguments is seen in Listing 3-2.
Listing 3-2. Code to plot a scatterplot where the title, subtitle, x axis label, y axis
label, x limits, and y limits have been set
plot(
LifeCycleSavings$pop75,
LifeCycleSavings$pop15,
main="Percentage under 15 versus Percentage over 75\nfor 50 Countries",
sub="More Under 15 Means Less Over 75 on Average",
xlab="Percentage over 75",
ylab="Percentage under 15",
xlim=c( 0, 5.25 ),
ylim=c( 18, 52 )
)
17
Chapter 3 The Arguments of plot( )
Figure 3-2. Scatterplot plotted using the arguments main, sub, xlab, ylab, xlim,
and ylim
18
Chapter 3 The Arguments of plot( )
3.2.2 B
ox Type, Aspect Ratio, Annotation,
and Expanded Plotting
Whether to include a border, the type of border to include, and the aspect ratio of a plot
can be changed. Also, annotation can be shut off, and points can be plotted outside the
limits of the plot. The relevant arguments are frame.plot for including a border, bty for
the box type, asp for the aspect ratio, ann for annotation, and xpd for expanded plotting.
The argument frame.plot is a logical single-value argument that tells R whether to
put a frame around the plot. If the argument is a vector of length greater than one, only
the first value is used, and a warning is given. The default value is TRUE.
If frame.plot is TRUE, the argument bty describes the type of box. The argument
takes character values and can take on the values “o”, “l”, “7”, “c”, “u”, “]”, and “n”. The value
“o” is the default and indicates a four-sided box. The first six values (the capital letter for
the letters) look like the shape of the box that the value creates.
The value “l” plots the left and bottom axes; the value “7” plots the right and top axes;
the value “c” plots the top, left, and bottom axes; the value “u” plots the left, bottom, and
right axes; and the value “]” plots the bottom, right, and top axes. The value “n” indicates
to not draw a box. Figure 3-3 shows the six options other than the default, “o”.
The following code created the six plots in Figure 3-3.
In Figure 3-3 are the plots generated by the code in Listing 3-3.
19
Random documents with unrelated
content Scribd suggests to you:
ungraded class her powers would have progressively deteriorated
and Katie “would be in darkness.”
The teacher who thus first fixed our attention upon these defective
children has long been a member of the settlement family. She has
carried us with her in her zeal for them, and we have come to see
that it is because the public conscience has been sluggish that
means and methods have not been more speedily devised toward
an intelligent solution of this serious social problem.
From the small beginnings of the experimental class in Henry
Street a separate department in the public schools was created in
1908, and this year (1915) there are 3,000 children throughout the
city under the care of specially trained teachers who have liberty to
adapt the school work to the children’s peculiar needs. All these
ungraded classes are under the direction of Miss Farrell.
Looking back upon the struggles to win formal recognition of the
existence of these children, who now so much engage the attention
of educators and scientists, we realize that our colleague’s devotion
to them, her power to excite enthusiasm in us, and her
understanding of the social implications of their existence, came
from a deep-lying principle that every human being, even the least
lovely, merits respectful consideration of his rights and his
personality.
Much is required of the public school teachers, and many of them
rise to every demand; but naturally, in so great a number, there are
some who do not recognize that theirs is the responsibility for
discovering the children who are not normal. Harry sits on our
doorsteps almost every day, ready to run errands, and harmless as
yet. Obviously defective, a “pronounced moron,” he was promoted
from class to class, and when one of his settlement friends called
upon the teacher to discuss Harry’s special needs, the teacher,
somewhat contemptuous of our anxiety, observed that “all that Harry
needed was a whipping.”
From one-half of one per cent. to two per cent. of children of
school age are, it is estimated, in need of special instruction because
of the quality or the imperfect functioning of their mental powers. The
public school has the power, and should exercise it, to bring within its
walls all the children physically and mentally competent to attend it. If
children are under intelligent observation, departures from the
normal can in many instances be recognized in time for training and
education according to the particular need. Long-continued
observation and record of the child are essential to intelligent
treatment of abnormalities concerning which there is even now very
little accurate information. Cumulative experience and data, such as
can be obtained only through the compulsory attendance at school
of the multitudes of children of this type, will finally give a basis for
scientific and humanitarian action regarding them.
Up to a certain period the
child’s helplessness demands
that every opportunity for
development be given him, but
that is not the whole of
society’s responsibility. The
time comes when the child’s
own interests and those of the
community demand the wisest,
least selfish, and most
statesmanlike action. Society
must state in definite terms its
right to be protected from the
hopelessly defective and the
moral pervert, wherever found.
This constitutes the real
problem of the abnormal. At
the adolescent period those
unfit for parenthood should be
guarded—girls and boys—and
society should be vested with
authority and power to
accomplish segregation, the
conditions of which should
attract and not repel.
Because so much needs to be said upon it, if anything is said at
all, I am loath to touch upon the one great obstacle to the effective
use of all the intelligence and the resources available for the well-
being of these children, the most baffling impediment to their and the
community’s protection, namely, the supreme authority of
parenthood, be it never so inefficient, avaricious, or even immoral.
The Typical Employment Record of One Child between the Ages of 14 and 16.
Bessie and Jimmy and Pippa and Esther and their little comrades
stir us to contribute our human documents to the propaganda
instituted in behalf of children. In this, as in other experiments at the
settlement, we do not believe that what we offer is of great
consequence unless the demonstrations we make and the
experience we gain are applicable to the problems of the community.
On no other single interest do the members of our settlement meet
with such unanimity. Years of concern about individual children might
in any case have brought this about, but irresistible has been the
influence exercised by Mrs. Florence Kelley, now and for many years
a member of the settlement family. She has long consecrated her
energies to securing protective legislation throughout the country for
children compelled to labor and, with the late Edgar Gardner Murphy,
of Alabama, suggested the creation of the National Child Labor
Committee. In its ten years’ existence it has affected legislation in
forty-seven states, which have enacted new or improved child labor
laws. On this and on the New York State Committee Mrs. Kelley and
I have served since their creation.
Though much has been accomplished during this decade, the field
is immensely larger than was supposed, and forces inimical to
reform, not reckoned with at first, have been encountered. Despite
this opposition, however, we believe that the abolition of child labor
abuses in America is not very far off.
In Pennsylvania, within a very few years, insistence upon
satisfactory proof of age was strenuously opposed. Officials who
should have been working in harmony with the committee persisted
in declaring that the parent’s affidavit, long before discarded in New
York State, was sufficient evidence, despite the fact that coroners’
inquests after mine disasters showed child workers of ten and eleven
years. The Southern mill children, the little cranberry-bog workers,
the oyster shuckers, and the boys in glass factories and mines have
shown that this disregard of children is not peculiar to any one
section of the country, though Southern states have been most
tenacious of the exemption of children of “dependent parents” or
“orphans” from working-paper requirements.
In the archives at Washington much interesting evidence lies
buried in the unpublished portions of reports of the federal
investigation into the work of women and children. The need of this
investigation was originally urged by settlement people. One mill
owner greeted the government inspectors most cordially and, to
show his patriotism, ordered the flag to be raised above the works.
The raising of the flag, as it afterwards transpired, was a signal to the
children employed in the mill to go home. In the early days of child
labor reform in New York the children on Henry Street would
sometimes relate vividly their experience of being suddenly whisked
out of sight when the approach of the factory inspector was signaled.
It is perhaps unnecessary to mention the obvious fact that the
child worker is in competition with the adult and drags down his
wages. At the Child Labor Conference held in Washington in
January, 1915, a manufacturer in the textile industry cited the wages
paid to adults in certain operations in the mills as fourteen cents per
hour where there were prohibitive child labor laws and eleven cents
an hour where there were none.
The National Child Labor Committee now asks Congress through
a federal bill to outlaw interstate traffic in goods produced by the
labor of children. Such a law would protect the public-spirited
employer who is now obliged to compete in the market with men
whose business methods he condemns.
Sammie and his brother sold papers in front of one of the large
hotels every night. The more they shivered with cold, the greater the
harvest of pennies. No wonder that the white-faced little boy stayed
out long after his cold had become serious. He himself asked for
admission to the hospital, and died there before his absence was
noted. After his death relatives appeared, willing to aid according to
their small means, and the relief society increased its stipend to his
family. At any time during his life this aid might have been
forthcoming, had not the public unthinkingly made his sacrifice
possible by the purchase of his papers.
Opposition to regulating and limiting the sale of papers by little
boys on the streets is hard to overcome. A juvenile literature of more
than thirty years ago glorified the newsboy and his improbable
financial and social achievements, and interest in him was
heightened by a series of pictures by a popular painter, wherein
ragged youngsters of an extraordinary cleanliness of face were
portrayed as newsboys and bootblacks. In opposition to the charm of
this presentation, the practical reformer offers the photographs,
taken at midnight, of tiny lads asleep on gratings in front of
newspaper offices, waiting for the early editions. He finds in street
work the most fruitful source of juvenile delinquency, with newsboys
heading the list.
I am aware that at this point numerous readers will recall instances
of remarkable achievements by the barefoot boy, the wide-awake
young news-seller. We too
have known the exceptional
lad who has accomplished
marvels in the teeth of,
sometimes because of, great
disadvantages; but after twenty
years I, for one, have no
illusions as to the outcome for
the ordinary child.
When the New York Child
Labor Committee secured the
enactment of a law making it
mandatory for the schoolboy
who desired to sell papers to
obtain the consent of his
parents before receiving the
permissive badge from the
district school superintendent,
we sent a visitor from the settlement to the families of one hundred
who had expressed their intention to secure the badge. Of these
families over sixty were opposed to the child’s selling papers on the
street. The boy wanted to “because the other fellows did,” and the
parents based their objections, in most cases, on precisely those
grounds urged by social workers,—namely, that street work led the
boys into bad company, irregular hours, gambling, and “waste of
shoe leather.” Some asserted that they received no money from the
children from the sale of the papers. On the other hand, a committee
of which I was chairman, which made city-wide inquiry into juvenile
street work, found instances of well-to-do parents who sent their little
children on the streets to sell papers, sometimes in violation of the
law.
The three chief obstacles to progress in protection of the children
are the material interests of the employers, many of whom still
believe that the child is a necessary instrument of profit; a
sentimental, unanalytical feeling of kindness to the poor; and the
attitude of officials upon whom the enforcement of the law depends,
but who are often tempted by appeals to thwart its humane purpose.
A truant officer of my acquaintance took upon himself discretionary
power to condone the absence of a little child from school on the
ground that the child was employed and the widowed mother poor.
Himself a tender father, cherishing his small son, I asked him if that
was what he would have me do in case he died and I found his child
at work. Oddly enough, he seemed then to realize for the first time
that those who were battling for school attendance for the children of
the poor and prevention of their premature employment, even though
the widow and child might have to receive financial aid, were trying
to take, in part, the place of the dead father.
To meet cases where enforcement of the new standards of the law
involves undeniable hardship, another form of so-called “scholarship”
is given by the New York Child Labor Committee. Upon investigation
a sum approximating the possible earnings of the child is furnished
until such time as he or she can legally go to work. An indirect but
important result of the giving of these scholarships has been the
continuous information obtained regarding enforcement of the school
attendance law. Inquiry into the history of candidates disclosed, at
first, many cases in which, although the family had been in New York
for years, some of the children had never attended school, and
perhaps never would have done so had they not been discovered at
work illegally. The number of these cases is now diminishing.
Allusion to these two forms of “scholarships” should not be made
without mention of one other in the settlement, known as the “Alva
Scholarship.” The interest on the endowment is used to promote the
training of gifted individuals and to commemorate a beloved club
leader. The money to establish it was given by the young woman’s
associates in the settlement, and small sums have been contributed
to it by the girls who were members of her own and other clubs.
CHAPTER VIII
THE NATION’S CHILDREN
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com