22
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ggplot2でアレやコレを消す

Posted at

背景色の消し方をよく忘れるのでメモ。

ベース

データセットはmpgを使う。

## エンジン排気量(displ)と高速燃費(hwy)で散布図を描き、
## 駆動方式(drv: f...前輪駆動 r...後輪駆動 4...四輪駆動)をpchにマッピングし
## メーカー(manufacturer)別に分割
p <- ggplot(mpg, aes(x = displ, y = hwy, pch = drv)) +
  geom_point() +
  facet_wrap(~manufacturer)
p

Rplot001.png

アイツを消し去りたい場合

各要素を個別に操作するにはtheme()を使い、theme(axis.line=...)のように指定する。どのような要素に指定できるかは?themeで確認。
このとき、直接値を指定する要素もあるが、多くはelement_関数を介して指定する。
とりあえず、要素を消してしまいたいときはelement_blank()を使う。

p + theme(
  panel.background = element_blank(),
  panel.grid = element_blank()
)

Rplot002.png

本気で消すつもりは無かった場合

element_シリーズにはblank以外にrect, line, textがあり、背景のように長方形っぽいものを細かく操作するにはelement_rect()を使う。

p + theme(
  panel.background = element_rect(fill = "transparent", colour = "black"),
  panel.grid = element_blank()
)

Rplot003.png

アレの名前が分からない

facetで分割した時のそれぞれのグラフの上につくラベルはstripという。

p + theme(
  panel.background = element_rect(fill = "transparent", colour = "black"),
  panel.grid = element_blank(),
  strip.background = element_blank()
)

Rplot004.png

コレの名前が分からない

legendのそれぞれの要素の背景部分はkeyで指定する。

p + theme(
  panel.background = element_rect(fill = "transparent", colour = "black"),
  panel.grid = element_blank(),
  strip.background = element_blank(),
  legend.key = element_blank()
)

Rplot005.png

22
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?