About

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

ICYMI: Using rtweet Package Is Super Easy Now!

February 12, 2018

Since the version 0.6.0 (published on Nov 16, 2017), rtweet package no longer requires the users to create their own apps. Did you notice? I didn’t know this until the author, Michael W. Kearney, kindly answered to my silly question on GitHub. I really appreciate that he is always eager not only to maintain the code but also to communicate with lazy users like me. Then, I did a quick poll (sorry, in Japanese) about how well known is the fact. ... Read more

dplyr Doesn't Provide Full Support For S4 (For Now?)

February 12, 2018

I’ve seen sooo many (duplicated) issues on this topic were opened on dplyr’s repo and lubridate’s repo. For example, you cannot filter() intervals. Consider the data below: library(lubridate) #> Loading required package: methods #> #> Attaching package: 'lubridate' #> The following object is masked from 'package:base': #> #> date library(dplyr, warn.conflicts = FALSE) # some method of tibble() won't work for interval d <- data.frame( i = interval(ymd(10000101) + years(1:3 * 1000), ymd(10000102) + years(1:3 * 1000)), value = 1:3 ) d #> i value #> 1 2000-01-01 UTC--2000-01-02 UTC 1 #> 2 3000-01-01 UTC--3000-01-02 UTC 2 #> 3 4000-01-01 UTC--4000-01-02 UTC 3 Let’s select the second row by filter(). ... Read more