/
bubble_blow.R
188 lines (179 loc) · 6.66 KB
/
bubble_blow.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#' Easily create circular packing charts
#'
#' This function allows you to create cool circular packing charts easily by providing just
#' the data and a template among the available ones. Moreover, you can choose whether and
#' where to display percentages and also optionally specify a title for the chart.
#'
#' @param data A data frame (or a tibble) with two variables (columns):\cr
#' - in the first one there must be the vector of labels;\cr
#' - in the second one there must be the vector of values.\cr
#' You can use \code{\link{pie_datacheck}} to understand if the data is suitable.
#' @param template The chosen template.\cr
#' The available templates for this function are 'bub1', 'bub2', 'bub3', 'bub4', 'bub5'.
#' @param perc You can choose among:\cr
#' - 'no' : the percentages won't be displayed (default choice);
#' - 'below' : the percentages will be displayed below the group labels;
#' - 'right' : the percentages will be displayed next to the group labels.
#' @param title A string. If you want, you can specify the title of the graph.
#'
#' @return
#' A ggplot object.
#' In particular, this function returns a circular packing chart according to the
#' data, the choice of the template, and the other specifications provided.
#'
#'@examples
#' example <- data.frame(
#' c("This", "Is", "Just", "An", "Example"),
#' c(2.9, 6.9, 4.20, 13.12, 6.66)
#' )
#' bubble_blow(
#' data = example,
#' template = "bub1",
#' perc = "below",
#' title = "Example1"
#' )
#'
#'example2 <- tibble::tibble(
#' c("cat 1", "cat2", "cat3", "cat4", "cat5", "cat6", "cat7", "cat8", "cat9"),
#' c(324, 432, 499, 291, 750, 836, 314, 133, 372)
#' )
#' bubble_blow(
#' data = example2,
#' template = "bub2",
#' perc = "right",
#' title = "Ex2"
#' )
#'
#'@seealso
#' See all the available templates displayed
#' \href{https://paolodalena.github.io/tastypie/articles/available_templates.html}{here}!
#'
#' @importFrom packcircles circleProgressiveLayout circleLayoutVertices
#' @export
bubble_blow <- function(
data,
template,
perc = "no",
title = ""
){
# useful checks:
pie_datacheck(data)
if(tibble::is_tibble(data)){
data <- as.data.frame(data)
}
names(data) <- c("group", "value")
if(template %in% pie_template_list_pro){
stop("\n The selected template must be used with the function pie_bake_pro().
The available templates for this function are 'bub1', 'bub2', 'bub3', 'bub4', 'bub5'.")
}
else if(template %in% pie_template_list){
stop("\n The selected template must be used with the function pie_bake().
The available templates for this function are 'bub1', 'bub2', 'bub3', 'bub4', 'bub5'.")
}
else if(!template %in% c("bub1", "bub2", "bub3", "bub4", "bub5")){
stop("\n The selected template does not exist.
The available templates for this function are 'bub1', 'bub2', 'bub3', 'bub4', 'bub5'.")
}
# add useful info
usf <- circleProgressiveLayout(data$value, sizetype='area')
data <- cbind(data, usf)
dat.gg <- circleLayoutVertices(usf, npoints=50)
dat.gg$value <- rep(data$value, each=51)
dat.gg$perc <- rep(data$value/sum(data$value), each=51)
# check for percentage label
if(perc == "below"){
data$group <- paste(
data$group,
scales::label_percent(accuracy = 1)(data$value/sum(data$value)),
sep = "\n")
}
else if(perc == "right"){
data$group <- paste(
data$group,
scales::label_percent(accuracy = 1)(data$value/sum(data$value)),
sep = " : ")
}
else if(perc == "no"){}
else{
message("The selected value for perc is not valid. You can choose between 'no', 'below' and 'right'.")
}
if(template == "bub1"){
ggplot() +
geom_polygon(data = dat.gg, aes(x, y, group = id, fill=value), colour = "black", alpha = 1, size=1) +
scale_fill_distiller(palette = "Set3", direction = 1 ) +
geom_label(data = data, aes(x, y, size=value, label = group), family = "mono") +
scale_size_continuous(range = c(2, 5)) +
theme_void() +
theme(
legend.position="none",
plot.title = element_text(
family = "mono", color="black", size=16, face="bold", hjust = 0.5)
) +
coord_equal()+
ggtitle(title)
}
else if(template == "bub2"){
ggplot() +
# Make the bubbles
geom_polygon(data = dat.gg, aes(x, y, group = id, fill=value), colour = "white", alpha = 2, size=2) +
scale_fill_distiller(palette = "Spectral", direction = 1 ) +
# Add text in the center of each bubble + control its size
geom_label(data = data, aes(x, y, size=value, label = group, fill = value), family = "mono") +
scale_size_continuous(range = c(2, 5)) +
theme_void() +
theme(
legend.position="none",
plot.title = element_text(
family = "mono", color="black", size=16, face="bold", hjust = 0.5)
) +
coord_equal()+
ggtitle(title)
}
else if(template == "bub3"){
ggplot() +
geom_polygon(data = dat.gg, aes(x, y, group = id, fill=value), colour = "darkred", alpha = 1, size=1) +
scale_fill_distiller(palette = "Oranges", direction = 1 ) +
geom_label(data = data, aes(x, y, size=value, label = group), colour = "darkred", family = "mono") +
scale_size_continuous(range = c(2, 5)) +
theme_void() +
theme(
legend.position="none",
plot.title = element_text(
family = "mono", color="darkred", size=16, face="bold", hjust = 0.5)
) +
coord_equal()+
ggtitle(title)
}
else if(template == "bub4"){
ggplot() +
geom_polygon(data = dat.gg, aes(x, y, group = id, fill=value), colour = "black", alpha = 2, size = 2) +
scale_fill_distiller(palette = "Greys", direction = 1 ) +
geom_label(data = data, aes(x, y, size=value, label = group), family = "mono") +
scale_size_continuous(range = c(2, 5)) +
theme_void() +
theme(
legend.position="none",
plot.background = element_rect(fill="black"),
plot.title = element_text(
family = "mono", color="white", size=16, face="bold", hjust = 0.5)
) +
coord_equal()+
ggtitle(title)
}
else if(template == "bub5"){
ggplot() +
geom_polygon(data = dat.gg, aes(x, y, group = id, fill=value), colour = "white", alpha = 1, size = 1) +
scale_fill_distiller(palette = "Paired", direction = 1 ) +
geom_label(data = data, aes(x, y, size=value, label = group), family = "mono") +
scale_size_continuous(range = c(2, 5)) +
theme_void() +
theme(
legend.position="none",
plot.background = element_rect(fill="black"),
plot.title = element_text(
family = "mono", color="white", size=16, face="bold", hjust = 0.5)
) +
coord_equal()+
ggtitle(title)
}
}