A Tip to Debug ggplot2

January 11, 2019

Since the tidyverse developer day is near, I share my very very secret technique to debug ggplot2. Though this is a very small thing, hope this helps someone a bit. ggplot2 is unbreakable! You might want to debug() the methods of Geoms or Stats. debug(GeomPoint$draw_panel) But, this is not effective because the geom_point() generates different instances, so their draw_panel are all different objects (c.f. R6 classes have debug method for this). ... Read more

How To Convert A Human To Waves By Magick Package

November 23, 2018

I saw this tweet about Mathematica last year, which naturally urged me to write the R version of this code. Mathematicaを使って,シュレーディンガーの顔をこのようなアニメーションにされたユーザの方がいらっしゃいます。コードも掲載されています。https://t.co/IDIzM8Xfy2 pic.twitter.com/ZTIbjtmXBm — Wolfram Japan (@WolframJapan) June 15, 2017 At that time, I faild because I didn’t know how to zoom images. But, now I know magick package. Let’s try again… Zoom images by magick We can enlarge a image by either image_resize(), image_scale(), or image_sample(). I don’t know about the details of the differences, but it seems image_resize() does better for my purpose. ... Read more


October 19, 2018

Hi! I’m Hiroaki Yutani. This blog is where I struggle to learn about R (and improve my poor English). My interests are mainly focused on data visualization, data manipulation and web APIs. I maintain several R packages: gghighlight: Highlight ggplot with predicates. datadogr: R Client for Datadog API kntnr: R Client for kintone API qiitr: R Interface to Qiita API estatapi: R Interface to e-Stat API, the one-stop service for official statistics of the Japanese government kokudosuuchi: R Interface to Kokudo Suuchi API, the GIS data service of the Japanese government

Quote While the Promise Is Hot!

October 18, 2018

Suppose we want to quote x when x is not NULL. The naive implementation would be like below. Here, y is for comparison. Do you understand why x and y are quoted differently? quote_x_and_y <- function(x, y) { if (is.null(x)) { stop("x is NULL!", call. = FALSE) } x <- rlang::enquo(x) y <- rlang::enquo(y) list(x, y) } x <- y <- 1 quote_x_and_y(x, y) #> [[1]] #> <quosure> #> expr: ^1 #> env: empty #> #> [[2]] #> <quosure> #> expr: ^y #> env: global This is because x is evaluated when is. ... Read more

geom_sf_text() and geom_sf_label() Are Coming!

October 10, 2018

ggplot2 v3.1.0 will be released soon (hopefully), so let me do a spoiler about a small feature I implemented, geom_sf_label() and geom_sf_text(). How can we add label/text with geom_sf()? geom_sf() is one of the most exciting features introduced in ggplot2 v3.0.0. It magically allows us to plot sf objects according to their geometries’ shapes (polygons, lines and points). But, for plotting them as some other shapes than the original ones, we cannot rely on geom_sf() so it needs a bit of data transformation beforehand. ... Read more

Double dispatch of S3 method

September 17, 2018

When I tried to define an S3 class that contains multiple ggplot objects, I’ve faced the lessor-know mechanism of S3 method dispatch, double dispatch. Problem Take a look at this example. manyplot class contains many plots, and displays them nicely when printted. library(ggplot2) set.seed(100) d1 <- data.frame(x = 1:100, y = cumsum(runif(100))) d2 <- data.frame(x = 1:100, y = cumsum(runif(100))) plot_all <- function(...) { l <- lapply(list(...), function(d) ggplot(d, aes(x, y)) + geom_line()) l <- unname(l) class(l) <- "manyplot" l } print. ... Read more

gghighlight 0.1.0 Is Released!

July 4, 2018

gghighlight 0.1.0 is on CRAN now! New features As I’ve introduced on the previous post, gghighlight now can highlight any Geoms with gghighlight(). Since this function supersedes the previous functions, gghighlight_line() and gghighlight_point() are now deprecated. A Vignette One more small news is, gghighlight got an introductory vignette. This is basically the shorter version of the previous post: Introduction to gghighlight

Re-introduction to gghighlight: Highlight ggplot2 with Predicates

June 16, 2018

Half a year ago, I’ve introduced gghighlight package. I didn’t expect so much R people get interested in my package. Thanks for your attention! But, please forget about that gghighlight; gghighlight has become far more powerful and simple! So, let me re-introduce about gghighlight. (Note that this version of gghighlight is not yet on CRAN at the time of this writing. Please install by devtools::install_github("yutannihilation/gghighlight") for the time being) ... Read more

Plot geom_sf() On OpenStreetMap Tiles

June 9, 2018

mapview is a very nice package to explore an sf object. It can overlay sf object on the map images: nc <- sf::read_sf(system.file("shape/nc.shp", package="sf")) mapview::mapview(nc) But, how can I do this with ggplot2? (My friend told me mapview::mapshot() can generate a PNG, but I want to do this with ggplot2!) RTFM Before anything, I need to read Tile Usage Policy to use the OpenStreetMap tiles. For “Requirements” section, this is important: ... Read more

Anatomy of gghighlight

June 3, 2018

I’m overhauling my gghighlight package toward the upcoming release of ggplot2 2.3.0. I think I will introduce about the new gghighlight() soon, but before that, I want to write out the ideas behind gghighlight. Note that what I’ll write here contains few new things, as the basic idea is already covered by this great post: Plotting background data for groups with ggplot2 My post is mainly for organizing my thought, yet I hope someone find this useful :) ... Read more