generated from jtr13/cctemplate
/
scatterplots_cheatsheet.Rmd
51 lines (34 loc) · 1.58 KB
/
scatterplots_cheatsheet.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Scatterplots cheatsheet
Shangzhi Liu
Check the link to see the scatterplots cheatsheet pdf file:
https://github.com/ShangzhiLiu2021/cc21fall2/blob/scatterplots_cheatsheet/resources/scatterplots_cheatsheet/scatterplots.pdf
```{r, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
# package used
library(ggplot2)
library(scatterplot3d)
library(car) #use data mtcars in package car
# Sample scatterplot 1:
set.seed(1234)
x <- rnorm(100,mean = 2, sd = 3)
y <- -1.5 + 2*x + rnorm(100)
df <- data.frame(x = x, y = y)
ggplot(data = df, aes(x = x, y = y)) + geom_point()
# Sample scatterplot 2:
ggplot(data = df, aes(x = x, y = y)) + geom_point(size = 3, alpha = 0.3, shape = 15, color = "blue", stroke = 0.3)
# 3D scatterplot:
scatterplot3d(mtcars$wt, mtcars$disp, mtcars$mpg, main="3D Scatterplot")
# 3D scatterplot example:
scatterplot3d(mtcars$wt, mtcars$disp, mtcars$mpg, highlight.3d=TRUE, type="h", main="3D Scatterplot")
# Density contour lines, Hexagonal and Square heatmap
ggplot(data = df, aes(x = x, y = y)) + geom_point() + geom_density2d(color = "red")
ggplot(data = df, aes(x = x, y = y)) + geom_point() + geom_hex(color = "red", bins = 30)
ggplot(data = df, aes(x = x, y = y)) + geom_point() + geom_bin_2d(color = "red", bins = 30)
# text
ggplot(mtcars, aes(wt, mpg)) + geom_point(color = 'red') + geom_text(aes(wt, mpg, label = rownames(mtcars)))
ggplot(mtcars, aes(wt, mpg)) + geom_point(color = 'red') + annotate("text", x = 3.840, y = 13.3, label = "Camaro Z28") + annotate("text", x = 1.935, y = 27.3, label = "Fiat X1-9")
#matrix
pairs(~mpg+disp+hp+drat+wt,data=mtcars)
```