-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scatterplot with densities.R
152 lines (126 loc) · 6.23 KB
/
Scatterplot with densities.R
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#--------------------------------
# Scatterplot with densities in R
#--------------------------------
library(ggplot2)
library(cowplot)
# 1. Create initial scatterplot
p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species))+
geom_point() +
labs(title = 'Scatterplot with marginal densities',
subtitle = 'Sepal.Length x Sepal.Width from Iris dataset',
y="Sepal.Width", x="Sepal.Length") +
theme(axis.text=element_text(size=8),
axis.title=element_text(size=8),
plot.subtitle=element_text(size=9, face="italic", color="darkred"),
panel.background = element_rect(fill = "white", colour = "grey50"),
panel.grid.major = element_line(colour = "grey90"))
# 2. Create marginal densities
xdens <- axis_canvas(p, axis = "x") +
geom_density(data = iris, aes(x = Sepal.Length, fill = Species),
alpha = 0.4, size = 0.2)
ydens <- axis_canvas(p, axis = "y", coord_flip = TRUE)+
geom_density(data = iris, aes(x = Sepal.Width, fill = Species),
alpha = 0.4, size = 0.2) + coord_flip()
p1 <- insert_xaxis_grob(p, xdens, grid::unit(.2, "null"), position = "top")
p2 <- insert_yaxis_grob(p1, ydens, grid::unit(.2, "null"), position = "right")
# 3. Create complete plot
ggdraw(p2)
# Multiple Scatterplots with densities
library(gridExtra)
# first plot -------------------------------------------------------------------
# 1. Create initial scatterplot
p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species))+
geom_point() +
labs(title = 'Scatterplot with marginal densities',
subtitle = 'Sepal.Length x Sepal.Width from Iris dataset',
y="Sepal.Width", x="Sepal.Length") +
theme(axis.text=element_text(size=8),
axis.title=element_text(size=8),
plot.subtitle=element_text(size=9, face="italic", color="darkred"),
panel.background = element_rect(fill = "white", colour = "grey50"),
panel.grid.major = element_line(colour = "grey90"))
# 2. Create marginal densities
xdens <- axis_canvas(p, axis = "x") +
geom_density(data = iris, aes(x = Sepal.Length, fill = Species),
alpha = 0.4, size = 0.2)
ydens <- axis_canvas(p, axis = "y", coord_flip = TRUE)+
geom_density(data = iris, aes(x = Sepal.Width, fill = Species),
alpha = 0.4, size = 0.2) + coord_flip()
p1 <- insert_xaxis_grob(p, xdens, grid::unit(.2, "null"), position = "top")
p2 <- insert_yaxis_grob(p1, ydens, grid::unit(.2, "null"), position = "right")
# 3. Create complete plot
plot1 <- ggdraw(p2)
# second plot ------------------------------------------------------------------
# 1. Create initial scatterplot
p <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species))+
geom_point() +
labs(title = 'Scatterplot with marginal densities',
subtitle = 'Sepal.Length x Petal.Length from Iris dataset',
y="Sepal.Width", x="Sepal.Length") +
theme(axis.text=element_text(size=8),
axis.title=element_text(size=8),
plot.subtitle=element_text(size=9, face="italic", color="darkred"),
panel.background = element_rect(fill = "white", colour = "grey50"),
panel.grid.major = element_line(colour = "grey90"))
# 2. Create marginal densities
xdens <- axis_canvas(p, axis = "x") +
geom_density(data = iris, aes(x = Sepal.Length, fill = Species),
alpha = 0.4, size = 0.2)
ydens <- axis_canvas(p, axis = "y", coord_flip = TRUE)+
geom_density(data = iris, aes(x = Petal.Length, fill = Species),
alpha = 0.4, size = 0.2) + coord_flip()
p1 <- insert_xaxis_grob(p, xdens, grid::unit(.2, "null"), position = "top")
p2 <- insert_yaxis_grob(p1, ydens, grid::unit(.2, "null"), position = "right")
# 3. Create complete plot
plot2 <- ggdraw(p2)
# third plot -------------------------------------------------------------------
# 1. Create initial scatterplot
p <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Species))+
geom_point() +
labs(title = 'Scatterplot with marginal densities',
subtitle = 'Sepal.Length x Petal.Width from Iris dataset',
y="Sepal.Width", x="Sepal.Length") +
theme(axis.text=element_text(size=8),
axis.title=element_text(size=8),
plot.subtitle=element_text(size=9, face="italic", color="darkred"),
panel.background = element_rect(fill = "white", colour = "grey50"),
panel.grid.major = element_line(colour = "grey90"))
# 2. Create marginal densities
xdens <- axis_canvas(p, axis = "x") +
geom_density(data = iris, aes(x = Sepal.Length, fill = Species),
alpha = 0.4, size = 0.2)
ydens <- axis_canvas(p, axis = "y", coord_flip = TRUE)+
geom_density(data = iris, aes(x = Petal.Width, fill = Species),
alpha = 0.4, size = 0.2) + coord_flip()
p1 <- insert_xaxis_grob(p, xdens, grid::unit(.2, "null"), position = "top")
p2 <- insert_yaxis_grob(p1, ydens, grid::unit(.2, "null"), position = "right")
# 3. Create complete plot
plot3 <- ggdraw(p2)
# fourth plot ------------------------------------------------------------------
# 1. Create initial scatterplot
p <- ggplot(iris, aes(x = Sepal.Width, y = Petal.Width, color = Species))+
geom_point() +
labs(title = 'Scatterplot with marginal densities',
subtitle = 'Sepal.Width x Petal.Width from Iris dataset',
y="Sepal.Width", x="Sepal.Length") +
theme(axis.text=element_text(size=8),
axis.title=element_text(size=8),
plot.subtitle=element_text(size=9, face="italic", color="darkred"),
panel.background = element_rect(fill = "white", colour = "grey50"),
panel.grid.major = element_line(colour = "grey90"))
# 2. Create marginal densities
xdens <- axis_canvas(p, axis = "x") +
geom_density(data = iris, aes(x = Sepal.Width, fill = Species),
alpha = 0.4, size = 0.2)
ydens <- axis_canvas(p, axis = "y", coord_flip = TRUE)+
geom_density(data = iris, aes(x = Petal.Width, fill = Species),
alpha = 0.4, size = 0.2) + coord_flip()
p1 <- insert_xaxis_grob(p, xdens, grid::unit(.2, "null"), position = "top")
p2 <- insert_yaxis_grob(p1, ydens, grid::unit(.2, "null"), position = "right")
# 3. Create complete plot
plot4 <- ggdraw(p2)
# final plot -------------------------------------------------------------------
final.plot <- grid.arrange(plot1, plot2, plot3, plot4, nrow = 2)
#----
# end
#----