Skip to content

Commit 4b4a159

Browse files
authored
Merge pull request #135 from MathMarEcol/devel
Add ability to reorder target plot
2 parents 81e99fe + b572f71 commit 4b4a159

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

R/splnr_featureRep.R

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,16 @@ splnr_plot_featureRep <- function(df,
190190
nr = 1,
191191
showTarget = NA,
192192
plotTitle = "",
193+
sort_by = "category",
193194
...) {
194195

196+
# TODO Add to documentation
197+
# sort_by = "category",
198+
# "difference",
199+
# "target",
200+
# "feature" "feature"
201+
# "representation" "relative_held"
202+
195203
assertthat::assert_that(
196204
inherits(df, c("data.frame", "tbl_df")),
197205
is.logical(renameFeatures),
@@ -259,10 +267,18 @@ splnr_plot_featureRep <- function(df,
259267
tibble::deframe()
260268

261269

270+
if (!sort_by %in% c("category", "feature", "target")){
271+
df <- df %>%
272+
dplyr::mutate(difference = relative_held - target,
273+
representation = relative_held)
274+
}
275+
276+
262277
gg_target <- ggplot2::ggplot() +
263278
ggplot2::geom_bar(data = df %>% dplyr::mutate(relative_held = dplyr::if_else(incidental == TRUE, NA, relative_held)),
264-
stat = "identity", position = "identity", ggplot2::aes(x = .data$feature, y = .data$relative_held,
265-
fill = .data$category, colour = .data$category),
279+
stat = "identity", position = "identity",
280+
ggplot2::aes(x = stats::reorder(.data$feature, .data[[sort_by]]), y = .data$relative_held,
281+
fill = .data$category, colour = .data$category),
266282
na.rm = TRUE) +
267283
ggplot2::geom_bar(data = df %>% dplyr::mutate(relative_held = dplyr::if_else(incidental == FALSE, NA, relative_held)),
268284
stat = "identity", position = "identity",
@@ -385,11 +401,11 @@ splnr_plot_circBplot <- function(df, legend_color, legend_list,
385401
repTarget = NA, colTarget = "red") {
386402

387403
assertthat::assert_that(
388-
length(unique(names(legend_color))) == length(legend_list),
389-
is.logical(indicateTargets),
390-
is.numeric(impTarget),
391-
is.numeric(repTarget),
392-
is.character(colTarget)
404+
length(unique(names(legend_color))) == length(legend_list),
405+
is.logical(indicateTargets),
406+
is.numeric(impTarget),
407+
is.numeric(repTarget),
408+
is.character(colTarget)
393409
)
394410

395411
# Adding rows to each group, creating space between the groups
@@ -441,8 +457,8 @@ splnr_plot_circBplot <- function(df, legend_color, legend_list,
441457

442458
# plotting the bars
443459
ggplot2::geom_bar(ggplot2::aes(x = as.factor(.data$id), y = .data$value, fill = .data$group),
444-
stat = "identity",
445-
position = "dodge"
460+
stat = "identity",
461+
position = "dodge"
446462
) +
447463

448464
# defining colors of the bars
@@ -486,14 +502,14 @@ splnr_plot_circBplot <- function(df, legend_color, legend_list,
486502
inherit.aes = FALSE
487503
) +
488504
ggplot2::annotate("text",
489-
x = rep(max(data$id - 1), 4),
490-
y = c(25, 50, 75, 100),
491-
label = c(25, 50, 75, 100),
492-
color = "grey50",
493-
size = 4,
494-
angle = 0, #-5
495-
fontface = "bold",
496-
hjust = 0.5
505+
x = rep(max(data$id - 1), 4),
506+
y = c(25, 50, 75, 100),
507+
label = c(25, 50, 75, 100),
508+
color = "grey50",
509+
size = 4,
510+
angle = 0, #-5
511+
fontface = "bold",
512+
hjust = 0.5
497513
) +
498514

499515
# setting limitations of actual plot

0 commit comments

Comments
 (0)