Learn R Programming

ggiraph (version 0.6.1)

geom_rect_interactive: interactive rectangles

Description

These geometries are based on geom_rect and geom_tile. See the documentation for those functions for more details.

Usage

geom_rect_interactive(mapping = NULL, data = NULL, stat = "identity",
  position = "identity", na.rm = FALSE, show.legend = NA,
  inherit.aes = TRUE, ...)

geom_tile_interactive(mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)

Arguments

mapping

The aesthetic mapping, see geom_point.

data

A data frame, see geom_point.

stat

The statistical transformation to use on the data for this layer, as a string, see geom_point.

position

Postion adjustment, see geom_point.

na.rm
show.legend
inherit.aes
...

other arguments passed on to layer. See geom_point.

See Also

ggiraph

Examples

Run this code
# NOT RUN {
# add interactive polygons to a ggplot -------
library(ggplot2)

dataset = data.frame( x1 = c(1, 3, 1, 5, 4),
	x2 = c(2, 4, 3, 6, 6),
	y1 = c( 1, 1, 4, 1, 3),
	y2 = c( 2, 2, 5, 3, 5),
	t = c( 'a', 'a', 'a', 'b', 'b'),
	r = c( 1, 2, 3, 4, 5),
	tooltip = c("ID 1", "ID 2", "ID 3", "ID 4", "ID 5"),
	uid = c("ID 1", "ID 2", "ID 3", "ID 4", "ID 5"),
	oc = rep("alert(this.getAttribute(\"data-id\"))", 5)
)

gg_rect = ggplot() +
	scale_x_continuous(name="x") +
	scale_y_continuous(name="y") +
	geom_rect_interactive(data=dataset,
		mapping = aes(xmin = x1, xmax = x2,
			ymin = y1, ymax = y2, fill = t,
			tooltip = tooltip, onclick = oc, data_id = uid ),
		color="black", alpha=0.5) +
	geom_text(data=dataset,
			aes(x = x1 + ( x2 - x1 ) / 2, y = y1 + ( y2 - y1 ) / 2,
					label = r ),
		size = 4 )


ggiraph(code = {print(gg_rect)})
library(ggplot2)
df <- data.frame(
  id = rep(c("a", "b", "c", "d", "e"), 2),
  x = rep(c(2, 5, 7, 9, 12), 2),
  y = rep(c(1, 2), each = 5),
  z = factor(rep(1:5, each = 2)),
  w = rep(diff(c(0, 4, 6, 8, 10, 14)), 2)
)
ggiraph( code = {
  print(
    ggplot(df, aes(x, y, tooltip = id)) + geom_tile_interactive(aes(fill = z))
  )
})


# correlation dataset ----
cor_mat <- cor(mtcars)
diag( cor_mat ) <- NA
var1 <- rep( row.names(cor_mat), ncol(cor_mat) )
var2 <- rep( colnames(cor_mat), each = nrow(cor_mat) )
cor <- as.numeric(cor_mat)
cor_mat <- data.frame( var1 = var1, var2 = var2,
  cor = cor, stringsAsFactors = FALSE )
cor_mat[["tooltip"]] <-
  sprintf("<i>`%s`</i> vs <i>`%s`</i>:</br><code>%.03f</code>",
  var1, var2, cor)

# ggplot creation and ggiraph printing ----
p <- ggplot(data = cor_mat, aes(x = var1, y = var2) ) +
  geom_tile_interactive(aes(fill = cor, tooltip = tooltip), colour = "white") +
  scale_fill_gradient2(low = "#BC120A", mid = "white", high = "#BC120A", limits = c(-1, 1)) +
  coord_equal()
ggiraph( code = print(p))
# }

Run the code above in your browser using DataLab