2017-10-31 17 views
0

棒グラフの最初の棒とx軸の左端との距離を指定する方法は不思議です最後のバーはx軸の右端にあります。私は何らかの理由で、プロットされる変数の数に応じて、距離がそれらに合うように変化することに気づいた。これは一般的には問題ありませんが、出版目的には適していません。棒グラフとx軸の端との距離を指定する方法

変数の数が同じである2つのプロットがありますが、ポイントのジッタが原因で、x軸の端に近いものが他方のポイントよりも近くなります。

enter image description here

enter image description here

+0

再現例とコード? –

答えて

1

私が正しくあなたを理解していれば、その後、scale_x_discreteexpand引数には何が必要です。あなたはここでそれについての詳細を読むことができます:http://ggplot2.tidyverse.org/reference/scale_discrete.htmlを:

「長さ2の数値ベクトルが与え乗法と加法拡張定数これらの定数は、データが軸からある距離離して配置されていることを確認してください。」

それは形式expand = c(MULTIPLICATIVE_VALUE, ADDITIVE_VALUE)で使用され、例が以下に提供される:

df <- data.frame(
    X = c("v1", "v2", "v3", "v4"), 
    Y = c(10, 2, 50, 20) 
) 

# Load Packages 
library(ggplot2) 
library(gridExtra) 

# Build standard plot 
plot1 <- ggplot(df, aes(X, Y, fill = X)) + 
    geom_bar(stat = "identity") + 
    labs(title = "Standard Plot") 

# Add expand argument 
plot2 <- plot1 + scale_x_discrete(expand = c(0, 1)) + 
    labs(title = "Expanded") 

# Compare 
grid.arrange(plot1, plot2) 

enter image description here

関連する問題