Skip navigation

ggplot() +
  geom_heart() +
  coord_equal() +
  labs(title="Happy Valentine's Day") +
  theme_heart()

Presented without exposition (since it’s a silly Geom)

This particular ❤️ math pilfered this morning from @dmarcelinobr:

library(ggplot2)

geom_heart <- function(..., colour = "#67001f", size = 0.5, fill = "#b2182b",
                       mul = 1.0, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) {
  
  data <- data.frame(t=seq(0, 10*pi, by=0.1))
  
  x <- function(t) 16*sin(t)^3
  y <- function(t) 13*cos(t) - 5*cos(2*t) - 2*cos(3*t) - cos(4*t)
  
  data$x <- x(data$t) * mul
  data$y <- y(data$t) * mul
  
  data <- rbind(data, data[1,])
  
  layer(
    data = data,
    mapping = aes(x=x, y=y),
    stat = "identity",
    geom = ggplot2::GeomPolygon,
    position = "identity",
    show.legend = show.legend,
    inherit.aes = inherit.aes,
    params = list(
      na.rm = na.rm,
      size = size,
      colour = colour,
      fill = fill,
      ...
    )
  )
  
}

theme_heart <- function() {
  ggthemes::theme_map(base_family = "Zapfino") +
    theme(plot.title=element_text(hjust=0.5, size=28)) +
    theme(plot.margin=margin(30,30,30,30))
}

3 Comments

  1. “`r
    library(emojifont)
    load.emojifont()

    library(ggplot2)
    geomheart <- function(heart=”kissingheart”, color=”#67001f”, size=120, x=1, y=1, …) {
    data <- data.frame(x=x, y=y, label=emoji(heart))
    geom_text(aes(x, y, label=label), data=data, family=”OpenSansEmoji”, color=color, size=size, …)
    }

    ggplot() + geomheart(“sparklingheart”) + theme_void()
    “`

    an emojifont version. :)

  2. Nicely done :-)

  3. Brilliant! Thanks for this :)


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.