forked from tinu-schneider/DBDA_hierach_diagram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_PNGs_of_MiniPlots.R
128 lines (102 loc) · 2.99 KB
/
plot_PNGs_of_MiniPlots.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
# DBAD diagram of hierarchical models
# Generate the Mini-Plots of different distributions
Manager <- list(mainLineColor = "skyblue",
mainLineWidth = 6,
baseLineWidth = 2,
directory = "./MiniPlots/",
miniPlots = c("Normal", "Gamma", "Beta", "Dirichlet", "Binomial", "Uniform", "Student"),
margin = c(0.5, 0.1, 0.1, 0.1),
dimPNG = c(width = 180, height = 60)
)
plotPNGsOfMiniPlots <- function() {
plots <- Manager$miniPlots
directory <- Manager$directory
.checkTarget(directory)
for (plot in plots) {
png(file = paste0(directory, plot, ".png"),
width = Manager$dimPNG["width"],
height = Manager$dimPNG["height"])
do.call(paste0(".plot", plot, "MiniPlot"), list())
dev.off()
}
}
.checkTarget <- function(directory) {
dir.create(directory, showWarnings = FALSE)
}
.plotNormalMiniPlot <- function() {
xLim <- 3.5
x <- seq(-xLim, xLim, length = 501)
y <- dnorm(x, 0, 1)
.generateMiniPlot(x, y)
}
.plotStudentMiniPlot <- function() {
xLim <- 3.5
x <- seq(-xLim, xLim, length = 501)
y <- dt(x, 1)
.generateMiniPlot(x, y)
}
.plotGammaMiniPlot <- function() {
x <- seq(0, 1, length = 502)
x <- x[-1] # without 0
y <- dgamma(x, 2, 6)
.generateMiniPlot(x, y)
}
.plotDirichletMiniPlot <- function() {
require(gtools)
x <- seq(0, 1, length = 502)
x <- x[-1] # without 0
x <- x[-501] # without 1
y <- ddirichlet(cbind(x, 1-x), c(.8, .8))
.generateMiniPlot(x, y)
}
.plotBetaMiniPlot <- function() {
x <- seq(0, 1, length = 503)
x <- x[-c(1, 503)] # without 0, 1
y <- dbeta(x, 7, 4)
.generateMiniPlot(x, y)
}
.plotBinomialMiniPlot <- function() {
x <- 0:10
y <- dbinom(x, 10, .5)
.generateMiniBarPlot(x, y)
}
.plotUniformMiniPlot <- function() {
x <- c(0, 0, 1, 1)
y <- c(0, .2, .2, 0)
.generateMiniPlot(x, y)
}
.generateMiniPlot <- function(x, y) {
op <- par(mar = Manager$margin)
yMin <- -0.06
ylims <- c(yMin, max(y))
plot(x, y,
type = "l",
axes = FALSE,
ylim = ylims,
col = Manager$mainLineColor,
lwd = Manager$mainLineWidth
)
abline(h = yMin, lwd = Manager$baseLineWidth, xpd = TRUE)
par(op)
}
.generateMiniBarPlot <- function(x, y) {
op <- par(mar = Manager$margin)
yMin <- -0.06
ylims <- c(yMin, max(y))
plot(x, y,
type = "h",
axes = FALSE,
ylim = ylims,
col = Manager$mainLineColor,
lwd = Manager$mainLineWidth
)
abline(h = yMin, lwd = Manager$baseLineWidth, xpd = TRUE)
par(op)
}
# show example
.plotNormalMiniPlot()
# generate all mini-plots
plotPNGsOfMiniPlots()
# clean up
rm("plotPNGsOfMiniPlots", "Manager")
# done