ggplot2の凡例やタイトルの色や位置を変更 -- opts()でできること
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)