LoginSignup
2
2

More than 5 years have passed since last update.

ggplot2で内向きに目盛り線を描く

Last updated at Posted at 2019-03-18

Rのggplot2パッケージは、デフォルトではグラフの目盛りは外向きになっていますが、向きと長さを変えることができます。この記事では、その方法を示します。
irisのグラフを使って、普通にグラフを描くと次のようになります。

plotiris.R
ggp <- ggplot(data=iris) +
  geom_point(aes(x=Sepal.Length, y=Petal.Length)) 
print(ggp)

image.png
グラフの目盛りは外側を向いています。
theme()を次のようにいじってやると、

plotiris2.R
ggp <- ggplot(data=iris) +
  geom_point(aes(x=Sepal.Length, y=Petal.Length)) + 
  theme(
    axis.ticks = element_line(colour = "black"),
    axis.ticks.length = unit(-3, "mm"),
    axis.text.x = element_text(margin = unit(rep(8,4), "mm")),
    axis.text.y = element_text(margin = unit(rep(8,4), "mm"))
  )
print(ggp)

image.png
目盛りは内向きになりました。
axis.ticks.length の数字をマイナスにすればいいです。ただし、その場合axis.text.x=~で目盛りと数字の距離を調整しないと適切な位置に目盛りの数字が表示されません。

僕はどっちかというと内向きのほうが好きです。

2
2
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
2
2