Skip to content

Commit 4d18696

Browse files
author
4n4kin
committed
Updated related files
1 parent 7a11e33 commit 4d18696

8 files changed

+68
-48
lines changed

ggplot2/01. Scatter Plots.html

Lines changed: 30 additions & 25 deletions
Large diffs are not rendered by default.

ggplot2/01. Scatter Plots.md

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1-
Basic Scatter Plot (*Nothing Fancy*)
2-
====================================
1+
Basic Scatter Plot (*Nothing Very Fancy - yet*)
2+
===============================================
33

44
We'll be using the `diamonds` dataset that comes with `ggplot2`.
55

66
###Basic Data Summary###
77

8-
```r
9-
require(ggplot2)
10-
require(gridExtra)
118
```
9+
## Loading required package: ggplot2
10+
## Loading required package: gridExtra
11+
## Loading required package: grid
12+
```
13+
1214

1315
```r
1416
diamonds<-diamonds
1517
summary(diamonds)
1618
```
1719

1820
```
19-
carat cut color clarity depth table price x y z
20-
Min. :0.200 Fair : 1610 D: 6775 SI1 :13065 Min. :43.0 Min. :43.0 Min. : 326 Min. : 0.00 Min. : 0.00 Min. : 0.00
21-
1st Qu.:0.400 Good : 4906 E: 9797 VS2 :12258 1st Qu.:61.0 1st Qu.:56.0 1st Qu.: 950 1st Qu.: 4.71 1st Qu.: 4.72 1st Qu.: 2.91
22-
Median :0.700 Very Good:12082 F: 9542 SI2 : 9194 Median :61.8 Median :57.0 Median : 2401 Median : 5.70 Median : 5.71 Median : 3.53
23-
Mean :0.798 Premium :13791 G:11292 VS1 : 8171 Mean :61.8 Mean :57.5 Mean : 3933 Mean : 5.73 Mean : 5.73 Mean : 3.54
24-
3rd Qu.:1.040 Ideal :21551 H: 8304 VVS2 : 5066 3rd Qu.:62.5 3rd Qu.:59.0 3rd Qu.: 5324 3rd Qu.: 6.54 3rd Qu.: 6.54 3rd Qu.: 4.04
25-
Max. :5.010 I: 5422 VVS1 : 3655 Max. :79.0 Max. :95.0 Max. :18823 Max. :10.74 Max. :58.90 Max. :31.80
26-
J: 2808 (Other): 2531
21+
## carat cut color clarity depth table price x y z
22+
## Min. :0.200 Fair : 1610 D: 6775 SI1 :13065 Min. :43.0 Min. :43.0 Min. : 326 Min. : 0.00 Min. : 0.00 Min. : 0.00
23+
## 1st Qu.:0.400 Good : 4906 E: 9797 VS2 :12258 1st Qu.:61.0 1st Qu.:56.0 1st Qu.: 950 1st Qu.: 4.71 1st Qu.: 4.72 1st Qu.: 2.91
24+
## Median :0.700 Very Good:12082 F: 9542 SI2 : 9194 Median :61.8 Median :57.0 Median : 2401 Median : 5.70 Median : 5.71 Median : 3.53
25+
## Mean :0.798 Premium :13791 G:11292 VS1 : 8171 Mean :61.8 Mean :57.5 Mean : 3933 Mean : 5.73 Mean : 5.73 Mean : 3.54
26+
## 3rd Qu.:1.040 Ideal :21551 H: 8304 VVS2 : 5066 3rd Qu.:62.5 3rd Qu.:59.0 3rd Qu.: 5324 3rd Qu.: 6.54 3rd Qu.: 6.54 3rd Qu.: 4.04
27+
## Max. :5.010 I: 5422 VVS1 : 3655 Max. :79.0 Max. :95.0 Max. :18823 Max. :10.74 Max. :58.90 Max. :31.80
28+
## J: 2808 (Other): 2531
2729
```
2830

2931
```r
@@ -34,27 +36,35 @@ colnames(diamonds)
3436
## [1] "carat" "cut" "color" "clarity" "depth" "table" "price" "x" "y" "z"
3537
```
3638

37-
```r
38-
nrow(diamonds)
39-
```
40-
41-
```
42-
## [1] 53940
43-
```
44-
4539
```r
4640
set.seed(56)
47-
data=diamonds[sample(1:nrow(diamonds),1000),]
41+
data=diamonds[sample(1:nrow(diamonds),2000),]
4842
```
4943

5044

5145
###Plots###
5246

47+
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:
50+
51+
+**Step 1:** Add dataset: `ggplot(data)`
52+
+**Step 2:** Add geometry(layer): `geom_point(x,y)`
53+
+**Step 3:** Add Plot title/change axes scales/faceting
54+
5355
####Plot 01####
5456

5557

58+
**Create a basic scatter plot:**
59+
Here:
60+
`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+
5666
```r
57-
p1=ggplot(data)+
67+
p1=ggplot(data)+
5868
geom_point(aes(y=price,x=carat))+
5969
ggtitle('p1: Basic scatter plot on diamonds dataset - Price vs Carats')
6070

@@ -65,6 +75,8 @@ p1
6575

6676
####Plot 02####
6777

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+
6880

6981
```r
7082
p2=ggplot(data)+
@@ -76,6 +88,9 @@ p2
7688

7789
![plot of chunk Scatter Plot-Color added](figure/Scatter Plot-Color added.png)
7890

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+
7994
####Plot 03####
8095

8196

6.5 KB
Loading
1.64 KB
Loading

ggplot2/figure/Combined Plots.png

6.51 KB
Loading
2.64 KB
Loading
2.99 KB
Loading
2.86 KB
Loading

0 commit comments

Comments
 (0)