Debug in R #1: The basics of interactive debugging



  • n (または空行でリターン): 現在の式を実行。
  • c (または cont): デバッグモードを終了して、以降の式を実行。
  • Q: デバッグモードを終了。以降の式は実行されない。
  • where: コールスタック(関数呼び出しのスタック)を表示。


Continue reading

Alpha version of colorbar legend in ggplot2

The examples below show colorbar legends in ggplot2.

Please note that this is alpha version and it is not sure if this implementation appears in the future version of ggplot2.

I’m happy if you play with this function and report potential bugs and missing functions.

To test this, you need install Hadley’s scales package and my implementation based on Hadley’s ggplot2.

Here is a direct link to the tarball:

The colorbar is controlled by passing the legend_param argument to scale_{fill, colour}_XXX. XXX needs to be continuous family, e.g., discrete, etc.

The legend_param needs to be a list. Currently three elements is available.

  1. colorbar (logical): draw colorbar if TRUE.
  2. colorbar_nbin (integer): the number of division of colorbar, i.e., the resolution of the colorbar default: approx. 20).
  3. colorbar_nbreaks (integer): the number of breaks of the colorbar (default: approx. 5).

The size of colorbar is controlled by theme – legend.key.size, legend.key.width, and legend.key.height.

The breaks and labels can be controlled via breaks and labels arguments in scale_*, this is same as normal legend.

Continue reading

Using CJK Fonts in R and ggplot2





d <- data.frame(x=1:2, y=1:2, t=c("あ","い"))

## PDFで出力する場合は簡単
p <- ggplot(d, aes(x, y, label=t)) + geom_text() + opts(title="う")
ggsave("out.pdf", p, family="Japan1GothicBBB")

## MacでQuartz(デフォルト)を使う場合
quartzFonts(HiraMaru=quartzFont(rep("HiraMaruProN-W4", 4)))
ggplot(d, aes(x, y, label=t)) + geom_text() + opts(title="う") + theme_grey(base_family="HiraMaru")
grid.gedit("GRID.text", grep=TRUE, global=TRUE, gp=gpar(fontfamily="HiraMaru"))

## Windowsでwindow(デフォルト)を使う場合
ggplot(d, aes(x, y, label=t)) + geom_text() + opts(title="う") + theme_grey(base_family="Mei")
grid.gedit("GRID.text", grep=TRUE, global=TRUE, gp=gpar(fontfamily="Mei"))


Continue reading

?: operator in R

In c/c++ or some other languages, ?: operator is useful to describe conditional statement:

printf("%d", 1?2:3); // -> 2
printf("%d", 0?2:3); // -> 3

In R, however, we cannot use ?: operator by default (of course, I know the function ifelse() and also if-else statement can return value).

Inspired by the tweet on #rstats, here is the definition of that operator in R:



> 1?1:2
[1] 1

> 0?1:2
[1] 2

> c(1,0,1)?c(1,2,3):c(5,5,5)
[1] 1 5 3

This is just a kind of joke hack.
I strongly recommend you not to use this trick.
This overwrites the default definition of `:` and `?`.

If you have trouble after defining `:` and `?` above, please undefine them:

rm(':', '?')

Now you can use default ‘:’ and ‘?’.

Japanese stock market in 2011



Continue reading