2016-07-14 27 views
2

私は、私は-4からプロットしたい、次の例のデータフレームを持っている-1:x軸の範囲を制限したまま、x軸のティックを変更するにはどうすればよいですか?

test_x <- c(-3.5, -2, -1, -0.5) 
test_y <- c(1,2,3,4) 
df <- data.frame(x=test_x, y=test_y) 
library(ggplot2) 
ggplot(df, aes(x=x, y=y)) + 
    geom_point() + 
    xlim(-4, -1) 

enter image description here

私は-4ダニを表示したいと私は-0.5ポイントを除外したいです。しかし、私はまた、x軸目盛のラベルを変更したい。連続データのために私はscale_x_continuous

ggplot(df, aes(x=x, y=y)) + 
    geom_point() + 
    scale_x_continuous(breaks=c(-4, -3, -2, -1), labels=c("a","b","c","d")) 

enter image description here

を発見したが、これはaダニを示していない、それは-0.5ポイントを排除するものではありません。 x_limで再度制限しようとすると、エラー Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scaleが返されます。

x軸の範囲を制限しながらx軸のティックを変更するにはどうすればよいですか?規模内部

+1

なぜあなたはそれをしたいのですか? 'x'が実際に連続している場合、x値に文字をラベリングすると、ポイント間の距離に関する情報が読み取られなくなります。 – adibender

答えて

4

使用制限:

ggplot(df, aes(x = x, y = y)) + 
    geom_point() + 
    scale_x_continuous(breaks = c(-4, -3, -2, -1), 
        labels = c("a", "b", "c", "d"), 
        limits = c(-4, -1)) 

、限界c(-4, -1)を適用することによって、私たちは一点を落としているので、私たちは警告を取得します:

警告メッセージ:削除含む1行を欠損値(geom_point)。

limitsに代わる方法として

、あなたも(その結果、あなたはまた、削除された行に関する警告を取得することはできません)ない制限を設定するように、基になるデータを変更しない coord_cartesian(xlim = c(-4, -1))を使用することができます
ggplot(df, aes(x=x, y=y)) + 
    geom_point() + 
    scale_x_continuous(breaks = c(-4, -3, -2, -1), 
        labels = c("a", "b", "c", "d")) + 
    coord_cartesian(xlim = c(-4, -1)) 
関連する問題