Skip to content

Commit

Permalink
plotly workarounds (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeehio committed Feb 26, 2023
1 parent 53ddb58 commit 0cc184c
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion R/plot-GCIMSSample.R
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,21 @@ overlay_peaklist <- function(
shape = "x"
)
)
} else {
# This else block is needed just because of plotly:
# plotly can't place an annotation_raster between geoms, it must be either below or
# above the whole plot.
# See https://github.com/sipss/GCIMS/issues/16
#
# When plotly finds a raster between geoms, chooses to place it "above" or "below"
# all of them by checking how many layers it has above and how many below, and picking the
# one with the majority (e.g. if the raster is the 3rd out of 4 layers it would be placed above)
# so to force it to appear below, we add a geom_blank on top, that will be invisible, but will
# make the annotation_raster to be 2/4 (First half) instead of 2/3 (second out of 3)
out <- c(
out,
ggplot2::geom_blank()
)
}
} else {
show_legend <- how_many_colors <= 10
Expand All @@ -549,7 +564,8 @@ overlay_peaklist <- function(
),
alpha = 0.3,
show.legend = show_legend
)
),
ggplot2::geom_blank()
)
if (has_apex) {
out <- c(
Expand All @@ -565,6 +581,21 @@ overlay_peaklist <- function(
show.legend = show_legend
)
)
} else {
# This else block is needed just because of plotly:
# plotly can't place an annotation_raster between geoms, it must be either below or
# above the whole plot.
# See https://github.com/sipss/GCIMS/issues/16
#
# When plotly finds a raster between geoms, chooses to place it "above" or "below"
# all of them by checking how many layers it has above and how many below, and picking the
# one with the majority (e.g. if the raster is the 3rd out of 4 layers it would be placed above)
# so to force it to appear below, we add a geom_blank on top, that will be invisible, but will
# make the annotation_raster to be 2/4 (First half) instead of 2/3 (second out of 3)
out <- c(
out,
ggplot2::geom_blank()
)
}
if (!is.null(palette)) {
palette <- unname(palette)
Expand All @@ -582,6 +613,15 @@ overlay_peaklist <- function(
ggplot2::scale_color_manual(values = palette)
)
}
# Even if we used show.legend = FALSE, plotly (4.10.1.9000) needs
# guides(colour = "none").
# So we force it here anyway:
if (!show_legend) {
out <- c(
out,
list(ggplot2::guides(colour = "none"))
)
}
}
out
}

0 comments on commit 0cc184c

Please sign in to comment.