13
12

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.

ggplotで数値軸の下限値(or上限値)だけを指定したい

Last updated at Posted at 2015-10-10

2016/04/12追記: ggplot2 v2.1.0にて動作を確認しました

Q

数値軸の下限を0にして、上限は設定しないというのはできないのでしょうか。例えば以下のようなコードのイメジです:

NG_example.R
+ scale_y_continuous(minlim=0)

A

1.0.0以降のバージョンであれば、下限値・上限値を設定するところにNAが使えます。以下をサンプルに設定します:

base_example.R
library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) +
  geom_point()

base_example.R-1.png

これに、scale_y_continuous(limits = c(0, NA))を加えます:

OK_example.R
ggplot(mtcars, aes(wt, mpg)) +
  geom_point() + 
  scale_y_continuous(limits = c(0, NA))

OK_example.R-1.png

また、ylim(c(0, NA))でも指定可能ですし、x軸についても同様です:

OK_example2.R
ggplot(mtcars, aes(wt, mpg)) +
  geom_point() + 
  ylim(c(0, NA)) +
  xlim(c(-2, NA))

OK_example2.R-1.png

0.9.xのバージョンを使っている場合は、expand_limits(y=0)が使用可能です:

OK_example3.R
ggplot(mtcars, aes(wt, mpg)) +
  geom_point() + 
  expand_limits(y=0)

OK_example3.R-1.png

もし1.0.0以降を使用しているなら、わかりやすさも含めて前者の方法を用いるといいでしょう。

参考

この記事は、StackOverflowに投稿された以下の記事を参考に翻訳し、一部内容を変更して作成しました:

13
12
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
13
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?