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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
#' Modify the colorbar
#'
#' @param p a plotly object
#' @param ... arguments are documented here
#' \url{https://plotly.com/r/reference/#scatter-marker-colorbar}.
#' @param limits numeric vector of length 2. Set the extent of the colorbar scale.
#' @param which colorbar to modify? Should only be relevant for subplots with
#' multiple colorbars.
#' @author Carson Sievert
#' @export
#' @examplesIf interactive() || !identical(.Platform$OS.type, "windows")
#'
#' p <- plot_ly(mtcars, x = ~wt, y = ~mpg, color = ~cyl)
#'
#' # pass any colorbar attribute --
#' # https://plotly.com/r/reference/#scatter-marker-colorbar
#' colorbar(p, len = 0.5)
#'
#' # Expand the limits of the colorbar
#' colorbar(p, limits = c(0, 20))
#' # values outside the colorbar limits are considered "missing"
#' colorbar(p, limits = c(5, 6))
#'
#' # also works on colorbars generated via a z value
#' corr <- cor(diamonds[vapply(diamonds, is.numeric, logical(1))])
#' plot_ly(x = rownames(corr), y = colnames(corr), z = corr) %>%
#' add_heatmap() %>%
#' colorbar(limits = c(-1, 1))
colorbar <- function(p, ..., limits = NULL, which = 1) {
colorbar_built(plotly_build(p), ..., limits = limits, which = which)
}
colorbar_built <- function(p, ..., limits = NULL, which = 1) {
isBar <- vapply(p$x$data, is.colorbar, logical(1))
if (sum(isBar) == 0) {
warning("Didn't find a colorbar to modify.", call. = FALSE)
return(p)
}
indicies <- which(isBar)[which]
for (i in indicies) {
tr <- p$x$data[[i]]
hasZcolor <- inherits(tr, "zcolor")
# retrain limits of the colorscale
if (!is.null(limits)) {
limits <- sort(limits)
if (hasZcolor) {
z <- p$x$data[[i]][["z"]]
if (!is.null(dz <- dim(z))) {
z <- c(z)
}
z[z < limits[1] | limits[2] < z] <- NA
if (!is.null(dz)) dim(z) <- dz
p$x$data[[i]]$z <- z
p$x$data[[i]]$zmin <- limits[1]
p$x$data[[i]]$zmax <- limits[2]
} else {
# since the colorscale is in a different trace, retrain all traces
# TODO: without knowing which traces are tied to the colorscale,
# there are always going to be corner-cases where limits are applied
# to more traces than it should
p$x$data <- lapply(p$x$data, function(x) {
type <- x[["type"]] %||% "scatter"
col <- x$marker[["color"]]
if (has_color_array(type, "marker") && is.numeric(col)) {
x$marker[["color"]][col < limits[1] | limits[2] < col] <- default(NA)
x$marker[["cmin"]] <- default(limits[1])
x$marker[["cmax"]] <- default(limits[2])
}
col <- x$line[["color"]]
if (has_color_array(type, "line") && is.numeric(col)) {
x$line[["color"]][col < limits[1] | limits[2] < col] <- default(NA)
x$line[["cmin"]] <- default(limits[1])
x$line[["cmax"]] <- default(limits[2])
}
x
})
}
}
# pass along ... to the colorbar
if (hasZcolor) {
p$x$data[[i]]$colorbar <- modify_list(tr$colorbar, list(...))
} else {
p$x$data[[i]]$marker$colorbar <- modify_list(tr$marker$colorbar, list(...))
}
}
p
}
#' Hide guides (legends and colorbars)
#'
#' @param p a plotly object.
#' @export
#' @seealso [hide_legend()], [hide_colorbar()]
#'
hide_guides <- function(p) {
hide_legend(hide_colorbar(p))
}
#' Hide color bar(s)
#'
#' @param p a plotly object.
#' @export
#' @seealso [hide_legend()]
#' @examplesIf interactive() || !identical(.Platform$OS.type, "windows")
#'
#' p <- plot_ly(mtcars, x = ~wt, y = ~cyl, color = ~cyl)
#' hide_colorbar(p)
#'
hide_colorbar <- function(p) {
p <- plotly_build(p)
for (i in seq_along(p$x$data)) {
trace <- p$x$data[[i]]
if (has_attr(trace$type, "showscale")) {
p$x$data[[i]]$showscale <- default(FALSE)
}
if (has_attr(trace$type, "marker")) {
p$x$data[[i]]$marker$showscale <- default(FALSE)
}
}
p
}
#' Hide legend
#'
#' @param p a plotly object.
#' @export
#' @seealso [hide_colorbar()]
#' @examplesIf interactive() || !identical(.Platform$OS.type, "windows")
#'
#' p <- plot_ly(mtcars, x = ~wt, y = ~cyl, color = ~factor(cyl))
#' hide_legend(p)
hide_legend <- function(p) {
if (ggplot2::is.ggplot(p)) {
p <- plotly_build(p)
}
p$x$.hideLegend <- TRUE
p
}
#' Convert trace types to WebGL
#'
#' @param p a plotly or ggplot object.
#' @export
#' @examplesIf interactive() || !identical(.Platform$OS.type, "windows")
#'
#' # currently no bargl trace type
#' toWebGL(ggplot() + geom_bar(aes(1:10)))
#' toWebGL(qplot(1:10, 1:10))
#'
toWebGL <- function(p) {
if (ggplot2::is.ggplot(p)) {
p <- plotly_build(p)
}
p$x$.plotlyWebGl <- TRUE
p
}
#' Create a complete empty plotly graph.
#'
#' Useful when used with [subplot()]
#'
#' @param ... arguments passed onto [plot_ly()]
#'
#' @export
plotly_empty <- function(...) {
eaxis <- list(
showticklabels = FALSE,
showgrid = FALSE,
zeroline = FALSE
)
layout(plot_ly(...), xaxis = eaxis, yaxis = eaxis)
}
#' Encode a raster object as a data URI
#'
#' Encode a raster object as a data URI, which is suitable for
#' use with `layout()` \href{https://plotly.com/r/reference/#layout-images}{images}.
#' This is especially convenient for embedding raster images on a plot in
#' a self-contained fashion (i.e., so they don't depend on external URL links).
#'
#' @param r an object coercable to a raster object via [as.raster()]
#' @param ... arguments passed onto [as.raster()].
#' @author Carson Sievert
#' @export
#' @references <https://plotly-r.com/embedding-images.html>
#' @examplesIf interactive() || !identical(.Platform$OS.type, "windows")
#'
#' # a red gradient (from ?as.raster)
#' r <- as.raster(matrix(hcl(0, 80, seq(50, 80, 10)), nrow = 4, ncol = 5))
#' plot(r)
#'
#' # embed the raster as an image
#' plot_ly(x = 1, y = 1) %>%
#' layout(
#' images = list(list(
#' source = raster2uri(r),
#' xref = "paper",
#' yref = "paper",
#' x = 0, y = 0,
#' sizex = 0.5, sizey = 0.5,
#' xanchor = "left", yanchor = "bottom"
#' ))
#' )
raster2uri <- function(r, ...) {
try_library("png", "raster2uri")
# should be 4 x n matrix
if (inherits(r, "nativeRaster")) {
# png::writePNG directly supports nativeRaster objects
png <- r
} else {
r <- grDevices::as.raster(r, ...)
rgbs <- col2rgb(c(r), alpha = T) / 255
nr <- dim(r)[1]
nc <- dim(r)[2]
reds <- matrix(rgbs[1, ], nrow = nr, ncol = nc, byrow = TRUE)
greens <- matrix(rgbs[2, ], nrow = nr, ncol = nc, byrow = TRUE)
blues <- matrix(rgbs[3, ], nrow = nr, ncol = nc, byrow = TRUE)
alphas <- matrix(rgbs[4, ], nrow = nr, ncol = nc, byrow = TRUE)
png <- array(c(reds, greens, blues, alphas), dim = c(dim(r), 4))
}
base64enc::dataURI(png::writePNG(png), mime = "image/png")
}
|