-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathggplot2_geometrics.R
More file actions
82 lines (56 loc) · 2.12 KB
/
ggplot2_geometrics.R
File metadata and controls
82 lines (56 loc) · 2.12 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Shown in the viewer:
ggplot(mtcars, aes(x = cyl, y = wt)) +
geom_point()
# Solutions:
# 1 - With geom_jitter()
ggplot(mtcars, aes(x = cyl, y = wt)) +
geom_jitter()
# 2 - Set width in geom_jitter()
ggplot(mtcars, aes(x = cyl, y = wt)) +
geom_jitter(width=0.1)
# 3 - Set position = position_jitter() in geom_point() ()
ggplot(mtcars, aes(x = cyl, y = wt)) +
geom_jitter(position = position_jitter(0.1))
# Jittering is useful when you have a discrete position, and a relatively
#' # small number of points
#' # take up as much space as a boxplot or a bar
ggplot(mtcars, aes(x = cyl, y = wt)) + geom_boxplot(color="grey50") +
geom_jitter(position = position_jitter(0.1))
#========Histograms=========
# Defaults to 30 bins
# many ways to find binwidth, use diff and range
# 1 - Make a univariate histogram
ggplot(mtcars, aes(x = mpg)) +
geom_histogram()
# 2 - Plot 1, plus set binwidth to 1 in the geom layer
ggplot(mtcars, aes(x = mpg)) +
geom_histogram(binwidth=1)
# 3 - Plot 2, plus MAP ..density.. to the y aesthetic (i.e. in a second aes() function)
ggplot(mtcars, aes(x = mpg)) +
geom_histogram(binwidth=1, aes(y=..density..))
# 4 - plot 3, plus SET the fill attribute to "#377EB8"
ggplot(mtcars, aes(x = mpg)) +
geom_histogram(binwidth=1, aes(y=..density..), fill="#377EB8")
#==========Bar plots============
mtcars$cyl <- factor(mtcars$cyl)
mtcars$am <- factor(mtcars$am)
# Draw a bar plot of cyl, filled according to am
ggplot(mtcars, aes(x = cyl, fill = am))+
geom_bar()
# Change the position argument to stack
ggplot(mtcars, aes(x = cyl, fill = am)) +
geom_bar(position="stack")
# Change the position argument to fill
ggplot(mtcars, aes(x = cyl, fill = am)) +
geom_bar(position="fill")
# Change the position argument to dodge
ggplot(mtcars, aes(x = cyl, fill = am)) +
geom_bar(position="dodge")
# Define posn_d with position_dodge()
posn_d <- position_dodge(width=0.2)
# Change the position argument to posn_d
ggplot(mtcars, aes(x = cyl, fill = am)) +
geom_bar(position = posn_d)
# Use posn_d as position and adjust alpha to 0.6
ggplot(mtcars, aes(x = cyl, fill = am)) +
geom_bar(position = posn_d, alpha = 0.6)