Is there a geom for a value_box in ggplot2?

I'm creating a prototype of a dashboard and I want to have a couple value boxes at the top with the details below via patchwork. I tried geom_label but it didn't work like I expected.
Would it be simpler to learn flexdashboard?

Hi @kieroneil. You can do this by combining goem_tile and goem_text. If image needed, use goem_image from ggimage package. The following is a sample code for you. You can play with the parameters to adjust the outlook.

library(tidyverse)

df <- data.frame(value = sample(1:10000, 6), lab = LETTERS[1:6], image = "https://www.r-project.org/logo/Rlogo.png", stringsAsFactors = FALSE)

tile_width <- 0.8
tile_height <- 0.5
value_nudge_x <- 0.7
value_nudge_y <- 1.1
value_font_size <- 5
value_font_color <- "white"
lab_nudge_x <- 0.7
lab_nudge_y <- 0.9
lab_font_size <- 4
lab_font_color <- "white"
img_nudge_x <- 1.2
img_nudge_y <- 1
img_size <- 0.2

ggplot(df) +
  geom_tile(aes(lab, 1, fill = lab), width = tile_width, height = tile_height) +
  ggimage::geom_image(aes(seq(img_nudge_x, length(image) + img_nudge_x - 1, length.out = length(image)), img_nudge_y, image = image), size = img_size) +
  geom_text(aes(seq(value_nudge_x, length(value) + value_nudge_x - 1, length.out = length(value)), value_nudge_y, label = value), color = value_font_color, size = value_font_size, hjust = 0) +
  geom_text(aes(seq(lab_nudge_x, length(lab) + lab_nudge_x - 1, length.out = length(lab)), lab_nudge_y, label = lab), color = lab_font_color, size = lab_font_size, hjust = 0) +
  coord_equal() +
  theme(axis.text = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank()) +
  guides(fill = FALSE) +
  theme_void()

Created on 2020-02-27 by the reprex package (v0.3.0)

2 Likes

Wow, that is impressive. I never heard of ggimage before either and can now add it to my visualization toolbox. Thanks for sharing.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.