Enhancing gather() and spread() by Using "Bundled" data.frames

February 3, 2019

table { width: 50%; font-size: 80%; } Last month, I tried to explain gather() and spread() by gt package (https://yutani.rbind.io/post/gather-and-spread-explained-by-gt/). But, after I implemented experimental multi-gather() and multi-spread(), I realized that I need a bit different way of explanation… So, please forget the post, and read this with fresh eyes! Wait, what is multi-gather() and multi-spread()?? In short, the current gather() and spread() have a limitation; they can gather into or spread from only one column at once. ... Read more

A Survival Guide To Install rlang From GitHub On Windows

January 25, 2019

I don’t have any strong feelings about OSs. They are just tools. I had been a Mac user for 10+ years since I was 10, and now I’m using Windows for no reason. All OSs have their pros and cons. For example, I like Mac, but, in the late 90s, I was very disappointed at Mac because it didn’t have fonts to display Shift_JIS art nicely. Anyway, I’m using Windows and I need to survive. ... Read more

gather() and spread() Explained By gt

January 24, 2019

table { width: 50%; font-size: 80%; } This is episode 0 of my long adventure to multi-spread and multi-gather (this is my homework I got at the tidyverse developer day…). This post might seem to introduce the different semantics from the current tidyr’s one, but it’s probably just because my idea is still vague. So, I really appreciate any feedbacks! tl;dr I now think gather() and spread() are about ... Read more

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