You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `ggplot2` package by *Hadley Wickham* follows a layer by layer plot building philosophy. In order to build a plot using `ggplot2` , the ideal way is to think in terms of plotting layers.
48
+
49
+
The steps involved in creating a plot are as follows:
`data` is a sample of 2000 observations from diamonds dataset.
61
+
The `null` plot is initialised by using `ggplot()`
62
+
To create a scatterplot, we use `geom_point()` as geometry/layer.
63
+
To add plot title we use `ggtitle()`
64
+
65
+
56
66
```r
57
-
p1=ggplot(data)+
67
+
p1=ggplot(data)+
58
68
geom_point(aes(y=price,x=carat))+
59
69
ggtitle('p1: Basic scatter plot on diamonds dataset - Price vs Carats')
60
70
@@ -65,6 +75,8 @@ p1
65
75
66
76
####Plot 02####
67
77
78
+
To create a scatter plot with colors according to a given factor variable, we use the `color` parameter. It takes in a factor (categorical) variable as input & colors the points according to the components of factor variable.
79
+
68
80
69
81
```r
70
82
p2=ggplot(data)+
@@ -76,6 +88,9 @@ p2
76
88
77
89

78
90
91
+
The above plot is exactly same as `p2=p1+geom_point(aes(x=carat,y=price,color=cut))`. If you already have a plot & intend to modify its one aspect using `ggplot2`, you can do it by using the `+` operator along with your plot's layer/geometry specification.
92
+
93
+
79
94
####Plot 03####
80
95
81
96
Collapse file: ggplot2/figure/Adding a variety of paramters.png
0 commit comments