1
1
# ' @eval get_description('annotation_pie_chart')
2
2
# ' @export
3
- annotation_pie_chart <- function (
4
- factor_name ,
5
- label_rotation = FALSE ,
6
- label_location = " inside" ,
7
- label_type = " percent" ,
8
- legend = FALSE ,
9
- pie_rotation = 0 ,
10
- centre_radius = 0 ,
11
- centre_label = NULL ,
12
- count_na = FALSE ,
13
- ... ) {
3
+ annotation_pie_chart <- function (factor_name ,
4
+ label_rotation = FALSE ,
5
+ label_location = " inside" ,
6
+ label_type = " percent" ,
7
+ legend = FALSE ,
8
+ pie_rotation = 0 ,
9
+ centre_radius = 0 ,
10
+ centre_label = NULL ,
11
+ count_na = FALSE ,
12
+ ... ) {
14
13
check <- centre_radius > = 0 & centre_radius < = 1
15
14
if (! check ) {
16
15
stop(" Pie chart centre_radius must be between 0 and 1." )
17
16
}
18
-
17
+
19
18
out <- struct :: new_struct(
20
19
" annotation_pie_chart" ,
21
20
factor_name = factor_name ,
@@ -29,7 +28,7 @@ annotation_pie_chart <- function(
29
28
count_na = count_na ,
30
29
...
31
30
)
32
-
31
+
33
32
return (out )
34
33
}
35
34
@@ -47,7 +46,7 @@ annotation_pie_chart <- function(
47
46
centre_radius = " entity" ,
48
47
centre_label = " entity" ,
49
48
count_na = " entity" ,
50
- .pie_data = ' data.frame'
49
+ .pie_data = " data.frame"
51
50
),
52
51
prototype = list (
53
52
name = " Annotation pie chart" ,
@@ -165,13 +164,13 @@ setMethod(
165
164
definition = function (obj , dobj ) {
166
165
r1 <- obj $ centre_radius
167
166
radians <- obj $ pie_rotation * pi / 180
168
-
167
+
169
168
if (obj $ label_location == " inside" ) {
170
169
offset <- 1
171
170
} else {
172
171
offset <- 1.6
173
172
}
174
-
173
+
175
174
if (obj $ count_na ) {
176
175
dobj $ data [[obj $ factor_name ]] <-
177
176
factor (dobj $ data [[obj $ factor_name ]])
@@ -180,8 +179,8 @@ setMethod(
180
179
df <- dobj $ data %> %
181
180
group_by(.data [[obj $ factor_name ]]) %> %
182
181
summarise(count = n()) %> %
183
- tidyr :: complete(.data [[obj $ factor_name ]],fill = list (count = 0 ))
184
-
182
+ tidyr :: complete(.data [[obj $ factor_name ]], fill = list (count = 0 ))
183
+
185
184
# labels
186
185
df $ label <- " "
187
186
if (obj $ label_type == " percent" ) {
@@ -191,7 +190,7 @@ setMethod(
191
190
} else if (obj $ label_type == " count" ) {
192
191
df $ label <- as.character(df $ count )
193
192
}
194
-
193
+
195
194
if (! obj $ legend ) {
196
195
if (obj $ label_type != " none" ) {
197
196
df $ label <- paste0(
@@ -202,7 +201,7 @@ setMethod(
202
201
df $ label <- df [[obj $ factor_name ]]
203
202
}
204
203
}
205
-
204
+
206
205
# position of label in degrees
207
206
df $ angle <- ((cumsum(df $ count ) - (df $ count / 2 )) / sum(df $ count )) *
208
207
360 - obj $ pie_rotation
@@ -211,13 +210,13 @@ setMethod(
211
210
# centre if at top or bottom
212
211
df $ hjust [df $ angle > 175 & df $ angle < 185 ] <- 0.5
213
212
df $ hjust [df $ angle > 355 | df $ angle < 5 ] <- 0.5
214
-
213
+
215
214
if (! obj $ label_rotation ) {
216
215
df $ rotate <- 0
217
216
} else {
218
217
df $ rotate <- df $ angle - 270 - (180 * df $ hjust )
219
218
}
220
-
219
+
221
220
# plot
222
221
g <- ggplot(
223
222
data = df ,
@@ -233,18 +232,18 @@ setMethod(
233
232
coord_polar(" y" , start = radians , clip = " off" ) +
234
233
theme_void() +
235
234
geom_text(aes(x = offset , label = .data [[" label" ]], angle = rotate ),
236
- position = position_stack(vjust = 0.5 ),
237
- hjust = df $ hjust ,
235
+ position = position_stack(vjust = 0.5 ),
236
+ hjust = df $ hjust ,
238
237
) +
239
238
theme(plot.margin = unit(c(1 , 1 , 1 , 1 ), " lines" )) +
240
239
xlim(c(0.5 , 1.6 )) +
241
240
scale_fill_Publication()
242
-
241
+
243
242
# legend
244
243
if (! obj $ legend ) {
245
244
g <- g + theme(legend.position = " none" )
246
245
}
247
-
246
+
248
247
# add centre label
249
248
if (! is.null(obj $ centre_label )) {
250
249
if (obj $ centre_label == " .total" ) {
@@ -255,8 +254,8 @@ setMethod(
255
254
g <- g +
256
255
geom_text(aes(x = 0.5 , y = 0 ), label = obj $ centre_label )
257
256
}
258
-
259
- obj @ .pie_data = df
257
+
258
+ obj @ .pie_data <- df
260
259
return (g )
261
260
}
262
261
)
0 commit comments