J

ggplot2の凡例やタイトルの色や位置を変更 -- opts()でできること

f:id:joker1110:20120217064709p:image:w360

ggplot2のスタイルの変更はopts()を使う

凡例の表示や背景の色など,さまざまな細かい設定が可能.
opts(XXX)のXXX部分に以下の文章を入力することで設定できる.

opts()で定義できる関数一覧

Axis Attributes

axis.text.x = theme_text()
axis.text.y = theme_text()
axis.title.x = theme_text()
axis.title.y = theme_text()
axis.line = theme_segment()
axis.ticks = theme_segment()
axis.ticks.length = unit()
axis.ticks.margin = unit()

Legend Attributes

legend.background = theme_rect()
legend.key = theme_rect()
legend.key.size = unit()
legend.key.width = unit()
legend.text = theme_text()
legend.title = theme_text()
legend.title.align (?)
legend.position = "none", "left", "right", "top", "bottom", or c(x, y)
legend.justification = "left", "right", "centre", or c(x,y)
legend.direction (?)
legend.box (?)

Graph/Panel Attributes

panel.background = theme_rect()
panel.border = theme_rect()
panel.margin = unit()
panel.grid.major = theme_line()
panel.grid.minor = theme_line()
plot.background = theme_rect()
title = "title name"
plot.title = theme_text()
plot.margin = unit()

Faceting Attributes

strip.background = theme_rect()
strip.text.x = theme_text()
strip.text.y = theme_text()

theme_XXX()

各項目に使用するtheme_text()とかは以下のとおり.
theme_text()

colour
size
angle
hjust
vjust
face = bold italic

theme_segment()

colour
size
linetype

theme_line()

colour
size
linetype

theme_rect()

colour
fill
size
linetype

opts()の利用例

たとえば,

g <- ggplot(iris, aes(Sepal.Length,Sepal.Width))
g <- g + geom_point(aes(shape=factor(Species)))
g <- g + opts(axis.line = theme_segment(colour='red', size=2, linetype=2),
              legend.position = "left",
              panel.background = theme_rect(colour="grey5",fill="pink",size=4,linetype=3),
              title = "opts() test",
              plot.title = theme_text(size=30,angle=45)
              )
print(g)

とすれば,
f:id:joker1110:20110726210329p:image:w360
となる.これでggplot2の自由度が増したはず.論文にも使えるかも.