2016-08-19 11 views
4

シーケンスを定義せずにggplotでブレークステップサイズを設定する方法はありますか?たとえば:シーケンスを定義せずに軸ブレークを変更する - ggplot

x <- 1:10 
y <- 1:10 

df <- data.frame(x, y) 

# Plot with auto scale 
ggplot(df, aes(x,y)) + geom_point() 

# Plot with breaks defined by sequence 
ggplot(df, aes(x,y)) + geom_point() + 
    scale_y_continuous(breaks = seq(0,10,1)) 

# Plot with automatic sequence for breaks 
ggplot(df, aes(x,y)) + geom_point() + 
    scale_y_continuous(breaks = seq(min(df$y),max(df$y),1)) 

# Does this exist? 
ggplot(df, aes(x,y)) + geom_point() + 
    scale_y_continuous(break_step = 1) 

あなたは、私が怠けているのですが、私がエラーバーの追加に私のseqminmax制限を変更しなければならなかったいくつかの機会があったと言うことがあります。だから、私はちょうど言ってみたい... xのブレークサイズを使って、自動的なスケールの制限がある。

答えて

3

独自の関数を定義して、breaks引数に渡すことができます。あなたのケースでうまくいくの例では、次に

ggplot(df, aes(x,y)) + geom_point() + scale_y_continuous(breaks = f) 

あなたは、例えば休憩、のステップを渡すために、これを修正することができ

enter image description here

与え

f <- function(y) seq(floor(min(y)), ceiling(max(y))) 

だろうその後、

f <- function(k) { 
     step <- k 
     function(y) seq(floor(min(y)), ceiling(max(y)), by = step)  
} 

ggplot(df, aes(x,y)) + geom_point() + scale_y_continuous(breaks = f(2)) 

など、2でダニとy軸を作成する4、...、10となる

独自のスケールを書き込むことによって、さらにこれを取ることができます機能

my_scale <- function(step = 1, ...) scale_y_continuous(breaks = f(step), ...) 

とちょうど

のようにそれを呼び出します
ggplot(df, aes(x,y)) + geom_point() + my_scale() 

素晴らしい仕事ニース1、

+0

:)私はそれが何かを壊してはならないと思いますが、あなたは確認することはできません。ありがとう – Pete900

関連する問題