1
00:00:00,560 --> 00:00:05,320
My name is Ashish Mahabal and we'll be
continuing with R and looking at ggplot.

2
00:00:05,320 --> 00:00:09,870
So, much of this material is taken from
Headley's book by the name ggplot2.

3
00:00:09,870 --> 00:00:13,580
We start with the anatomy of a plot.

4
00:00:13,580 --> 00:00:18,160
Last time we saw a little bit of what
grammar of graphics is, so this shows

5
00:00:18,160 --> 00:00:23,640
graphically how the elements in the
grammar of graphics can be brought about.

6
00:00:23,640 --> 00:00:28,010
So you see at the top, that you have
mapping of variables to aesthetics.

7
00:00:28,010 --> 00:00:33,230
So overall, what happens, is that you have
a data set, a data frame as we saw, and

8
00:00:33,230 --> 00:00:37,350
a certain set of mappings, one or more,
and then there are multiple layers.

9
00:00:38,580 --> 00:00:42,610
And for each of those layer,
you again have some data which is

10
00:00:42,610 --> 00:00:46,000
very likely a subset of
the original data that you have.

11
00:00:46,000 --> 00:00:50,830
Some mapping, and associated with that,
a geom and a stat and

12
00:00:50,830 --> 00:00:55,210
a position where that particular layer or
subset should go.

13
00:00:56,400 --> 00:01:01,059
Then, for the overall plot,
you have got one scale for

14
00:01:01,059 --> 00:01:06,020
mapping, and then you've got a coordinate
system and a faceting specification.

15
00:01:06,020 --> 00:01:09,190
So the coordinate system would take
care of whether you have log scale or

16
00:01:09,190 --> 00:01:10,960
linear scale and so on.

17
00:01:10,960 --> 00:01:14,555
And faceting specification is
where you will know what kind of

18
00:01:14,555 --> 00:01:18,780
categorical variables are being used
to separate the data sets into.

19
00:01:18,780 --> 00:01:21,550
Either sublayers or subplots and so on.

20
00:01:21,550 --> 00:01:27,280
So this overall picture is very good for
allowing you to debug your plot.

21
00:01:27,280 --> 00:01:31,150
So if you are trying to build a plot using
layers and you're not getting what you

22
00:01:31,150 --> 00:01:34,254
want, then you can ask yourself
whether you have the correct geom,

23
00:01:34,254 --> 00:01:35,960
whether you have got the correct stat, and

24
00:01:35,960 --> 00:01:39,360
how they should be interacting with each
other, where should the position be,

25
00:01:39,360 --> 00:01:42,580
whether that is correct, is one of your
coordinate system wrong and son on.

26
00:01:42,580 --> 00:01:45,180
So by just keeping this
figure in front of you,

27
00:01:45,180 --> 00:01:49,770
many times it's fairly easy to debug
what is it that you want to look at.

28
00:01:49,770 --> 00:01:52,570
Se there it is again a summary
of what a plot is made of.

29
00:01:52,570 --> 00:01:56,740
Again, there is a data set, a set of
mappings besides geom set and so on.

30
00:01:56,740 --> 00:01:57,980
Right?

31
00:01:57,980 --> 00:02:01,190
And so, example of scale can be this.

32
00:02:01,190 --> 00:02:04,510
It can be a continuous variable or
a discrete variable.

33
00:02:04,510 --> 00:02:09,940
Remember that, that discrete variables
are the ones that we use for doing

34
00:02:09,940 --> 00:02:15,260
categorization and faceting, but even
the continuous variables, you can convert

35
00:02:15,260 --> 00:02:21,440
them using the as.factor function into
a discrete variable and use that as well.

36
00:02:21,440 --> 00:02:26,010
So, what is it that actually
happens when you make an object?

37
00:02:26,010 --> 00:02:31,090
We saw that you can assign your ggplot
command to an object called b and

38
00:02:31,090 --> 00:02:34,480
not have anything rendered
immediately onto the screen.

39
00:02:34,480 --> 00:02:37,340
So what is it that you can
do with such an object?

40
00:02:37,340 --> 00:02:41,930
If b is the object that you have made,
then you can print it.

41
00:02:41,930 --> 00:02:44,075
And this is something that have
been scored automatically if

42
00:02:44,075 --> 00:02:45,460
you're not inside a loop.

43
00:02:45,460 --> 00:02:49,690
Say if you make this object and
simply say P on the command line.

44
00:02:49,690 --> 00:02:52,170
It's going to do the printing of P.

45
00:02:53,410 --> 00:02:55,070
Then you can save that object.

46
00:02:55,070 --> 00:02:57,890
So if you save it will
write out an image for you.

47
00:02:57,890 --> 00:02:59,810
So, rather than going to the screen,

48
00:02:59,810 --> 00:03:03,350
this is what will allow you
to write it out to the disk.

49
00:03:03,350 --> 00:03:07,400
The other interesting thing is summary,
it is as before or loaded and with

50
00:03:07,400 --> 00:03:12,390
this object b if you simply, say summary
with empty parentheses, then you're going

51
00:03:12,390 --> 00:03:17,170
to see what is hidden in the object and
we'll do that to all the objects.

52
00:03:17,170 --> 00:03:21,430
Similarly, save instead of ggsave
is going to save the object itself,

53
00:03:21,430 --> 00:03:22,900
rather than as a plot.

54
00:03:22,900 --> 00:03:28,060
The object as a R object will be saved to
the disc which you can reload later on.

55
00:03:28,060 --> 00:03:31,430
So let's look at the summary of
the P object that we have defined at

56
00:03:31,430 --> 00:03:32,120
the top here.

57
00:03:32,120 --> 00:03:34,970
Where we are again taking
the crts data set and

58
00:03:34,970 --> 00:03:38,350
simply plotting amplitude versus
standard deviation there and

59
00:03:38,350 --> 00:03:42,800
factoring it into different
colors by the object variable.

60
00:03:42,800 --> 00:03:47,020
So you see that though we have used
only amplitude and standard deviation in

61
00:03:47,020 --> 00:03:51,030
the plot the object knows about all
the variables that are in the data frame.

62
00:03:51,030 --> 00:03:53,040
So all of them are listed here.

63
00:03:53,040 --> 00:03:55,240
It also tells you what is
the size of the data frame.

64
00:03:55,240 --> 00:03:57,300
There are 1619 rows in
total with 26 variables.

65
00:03:57,300 --> 00:04:01,530
And then it tells you what
the mapping is for the X's or

66
00:04:01,530 --> 00:04:04,849
the Y's and
what facetting had been used here.

67
00:04:05,870 --> 00:04:10,532
Now in addition to p, if you were
to do a geom_point, like let's do p

68
00:04:10,532 --> 00:04:15,210
+ geom_point and look for the summary
of that, you'll additionally see what,

69
00:04:15,210 --> 00:04:19,340
the stat_identity is, what the geom_point
is, and what the position_identity is.

70
00:04:19,340 --> 00:04:23,580
In this case we have not specified that
explicitly, so we get mostly NULLs, and

71
00:04:23,580 --> 00:04:24,320
FALSE, and so on.

72
00:04:24,320 --> 00:04:26,970
But all that information
in principle is there.

73
00:04:26,970 --> 00:04:30,910
So that is another useful
thing about the laring aspect.

74
00:04:30,910 --> 00:04:34,410
You can keep on piling up various
layers into an object, and

75
00:04:34,410 --> 00:04:38,170
then those layers become available
to you in various different ways.

76
00:04:39,580 --> 00:04:43,850
So, you can say save p and
file name to save the data file, and

77
00:04:43,850 --> 00:04:47,070
then you can reload it again
using the load command.

78
00:04:47,070 --> 00:04:49,500
And then after that you can
add various layers to that,

79
00:04:49,500 --> 00:04:52,340
so, that's a very convenient thing to do.

80
00:04:52,340 --> 00:04:56,440
This is another way
example of adding layers.

81
00:04:56,440 --> 00:05:00,670
So we use here ggplot, and we are using
a slightly different set here.

82
00:05:00,670 --> 00:05:06,040
The set is called diamonds, and
it is available with the ggplot2 library.

83
00:05:06,040 --> 00:05:10,330
So if you have started ggplot2 library
the data diamonds will allow you

84
00:05:10,330 --> 00:05:12,000
to access these commands.

85
00:05:12,000 --> 00:05:17,420
So here we take that set of diamonds and
use the aesthetics of number of carats

86
00:05:17,420 --> 00:05:24,100
in that and price, and then use a cut for
the colors into the plot there.

87
00:05:24,100 --> 00:05:29,630
And so now if we do geom of
you can add that as a layer.

88
00:05:29,630 --> 00:05:32,060
Notice here that we have used
slightly different syntax.

89
00:05:32,060 --> 00:05:33,530
Rather than saying gm_point,

90
00:05:33,530 --> 00:05:37,770
we say that we want a layer and
then geom equal to point.

91
00:05:37,770 --> 00:05:42,570
Now, this is where you can start combining
different stats in geoms effectively.

92
00:05:42,570 --> 00:05:47,670
So, next what we are saying is that we can
simply plot diamonds with aesthetics of

93
00:05:47,670 --> 00:05:51,150
x equal to carrot, so it's simply
going to use that particular variable.

94
00:05:52,430 --> 00:05:55,770
But then to that p,
to that object that we just defined,

95
00:05:55,770 --> 00:06:00,140
we can add a layer with a separate
geom and a separate stat.

96
00:06:00,140 --> 00:06:03,520
So when you plot a histogram,
what is it that is actually happening?

97
00:06:03,520 --> 00:06:07,070
You bend the data, so
that is what you're, statistics is.

98
00:06:07,070 --> 00:06:09,010
And what do you actually plot?

99
00:06:09,010 --> 00:06:12,130
You plot bars of it, so
that is what your geom is,

100
00:06:12,130 --> 00:06:16,850
and so this pairing is automatically
done from, for histogram.

101
00:06:16,850 --> 00:06:20,330
And then when you say simply p,
that is what is going to appear for

102
00:06:20,330 --> 00:06:22,100
you on the screen.

103
00:06:22,100 --> 00:06:25,583
So here is an example of a histogram
as it comes out of ggplot2.

104
00:06:26,900 --> 00:06:29,614
In this case we have
use a binwidth of 0.3.

105
00:06:31,780 --> 00:06:35,080
We can of course use quite interesting and

106
00:06:35,080 --> 00:06:39,030
sometimes useless stats, or geoms for
a particular data set here.

107
00:06:39,030 --> 00:06:42,140
I am simply listing a few
different geoms like bar,

108
00:06:42,140 --> 00:06:45,140
boxplot, contour, line, point, step, text.

109
00:06:45,140 --> 00:06:47,390
There are a total 29 of these,
and just for

110
00:06:47,390 --> 00:06:52,600
fun here, I have got, used the geom of
polygons on this particular data set.

111
00:06:52,600 --> 00:06:57,480
This is the same dataset that we used in
the last lecture, the CIT six class set.

112
00:06:57,480 --> 00:06:59,520
And then you can see that
it doesn't make sense.

113
00:06:59,520 --> 00:07:03,690
But the shape is roughly the same as what
we had got when we had plotted x versus y.

114
00:07:05,400 --> 00:07:08,060
Similarly, there are 15
different statistics.

115
00:07:08,060 --> 00:07:09,290
I have listed a few here.

116
00:07:09,290 --> 00:07:11,390
The book and
various websites will have more.

117
00:07:11,390 --> 00:07:16,650
We will also have it on the website of
the course, so we can have contours there,

118
00:07:16,650 --> 00:07:20,420
we can have density2d, and
that is what I've used here.

119
00:07:20,420 --> 00:07:25,930
I've used density2d, and I've said that
let use bandwidth that is very small,

120
00:07:25,930 --> 00:07:31,840
only 0.01, and that is why you see that
towards the lower parts of xy area,

121
00:07:31,840 --> 00:07:34,280
you see that there
are very dense contours.

122
00:07:34,280 --> 00:07:38,220
As you go out, there is less density in,
you can see that in the contours there.

123
00:07:40,080 --> 00:07:42,440
So some additional points.

124
00:07:42,440 --> 00:07:46,170
You can have the ggplot
with geom_point as we saw.

125
00:07:46,170 --> 00:07:47,910
When you want to add aesthetics,

126
00:07:47,910 --> 00:07:53,050
you can give that as as
arguments to the geom_point 2.

127
00:07:53,050 --> 00:07:55,450
So, if you say geon.color equals to red,

128
00:07:55,450 --> 00:07:58,420
then the points are going
to be plotted as red.

129
00:07:58,420 --> 00:08:02,686
Similarly, in the layers,
you can have various smoothing things.

130
00:08:02,686 --> 00:08:05,465
So here are shown that for
the point method so

131
00:08:05,465 --> 00:08:10,170
that you can provide method
equal to lm as your layer, and

132
00:08:10,170 --> 00:08:14,610
then smoothing can also be done addition,
in addition to that.

133
00:08:15,640 --> 00:08:18,190
Similarly, you can do, scales and axes.

134
00:08:18,190 --> 00:08:21,080
We have not seen an example of that,
but that's homework.

135
00:08:21,080 --> 00:08:22,380
You can take a look at that.

136
00:08:22,380 --> 00:08:24,760
We are adding that as additional layers.

137
00:08:24,760 --> 00:08:28,370
So when we say geom point, it's going
to take whatever the default scale is.

138
00:08:28,370 --> 00:08:31,515
But in addition, you say that oh,
I want log scale for x,

139
00:08:31,515 --> 00:08:34,980
that's sure enough add that as a layer and
you get that.

140
00:08:34,980 --> 00:08:38,680
And this particular example,
we have added both x and y as log scale.

141
00:08:40,280 --> 00:08:42,530
Then similarly,
you can add plot options, so

142
00:08:42,530 --> 00:08:44,780
you don't have to be
stuck with the defaults.

143
00:08:44,780 --> 00:08:50,223
The defaults are reasonable, but normally
for making plots into publications, so

144
00:08:50,223 --> 00:08:54,640
you want the access and labels et cetera,
to be much bigger, so you can specify all

145
00:08:54,640 --> 00:08:58,470
of that as a additional layer simply
by using the command called opts.

146
00:08:58,470 --> 00:09:00,500
Here, I have specified for instance,

147
00:09:00,500 --> 00:09:03,520
what the title should be and
what the aspect ratio should be.

148
00:09:03,520 --> 00:09:04,990
And you can add various
other commands with that.

149
00:09:06,900 --> 00:09:12,890
So, the homework with respect to this is,
take the dataset that we have provided and

150
00:09:12,890 --> 00:09:17,410
we saw examples using only two variables,
amplitude and standard deviation.

151
00:09:17,410 --> 00:09:20,210
But there are several other
interesting variables.

152
00:09:20,210 --> 00:09:24,900
Try to see if you can use them and
separate them in many different ways.

153
00:09:24,900 --> 00:09:28,570
This, what we are covered here
is fairly basic graphics.

154
00:09:28,570 --> 00:09:33,600
We were not trying to visualize
in any very different or

155
00:09:33,600 --> 00:09:35,860
specific way how to go about things.

156
00:09:35,860 --> 00:09:38,020
We were looking at only what is available.

157
00:09:38,020 --> 00:09:41,080
There will be additional modules in
the course where you'll be looking at

158
00:09:41,080 --> 00:09:44,620
visualization, how you should
be plotting the data, et cetera.

159
00:09:45,930 --> 00:09:46,430
Thank you.

