使用一些示例數據生成一個情節是(我認爲)顯示您所遇到的問題。
library(ggplot2)
df <- data.frame(val = c(10, 20, 100, 5), name = LETTERS[1:4])
ggplot(df, aes(x = name, y = val, fill = name)) +
geom_bar(stat = "identity")
有從零點在y軸(杆的底部),並且其中x軸標籤是一間隙。
ggplot(df, aes(x = name, y = val, fill = name)) +
geom_bar(stat = "identity") +
scale_y_discrete(expand = c(0,0)) +
scale_x_discrete(expand = c(0,0))
這讓劇情:
注意我
爲此,可以使用scale_y_discrete
或scale_y_continuous
,取決於您的數據的性質,並設置expand
到c(0,0)
刪除我們也沿y軸去除了缺口,只需刪除scale_x_discrete
一行即可重新添加此缺口。
由於誤差條帶是一個問題,這裏有幾個例子:
ggplot(df, aes(x = name, y = val, fill = name)) +
geom_bar(stat = "identity") +
geom_errorbar(aes(ymin = val - 10,
ymax = val + 10))
你可以用規模來降低去除填充到錯誤吧:
ggplot(df, aes(x = name, y = val, fill = name)) +
geom_bar(stat = "identity") +
geom_errorbar(aes(ymin = val - 10,
ymax = val + 10)) +
scale_y_continuous(expand = c(0,0))
或者您可以使用coord_cartesian給出一個硬切斷:
ggplot(df, aes(x = name, y = val, fill = name)) +
geom_bar(stat = "identity") +
geom_errorbar(aes(ymin = val - 10,
ymax = val + 10)) +
scale_y_continuous(expand = c(0,0)) +
coord_cartesian(ylim = c(0, max(df$val) + 10))
你能不能做一個重複的例子?可能你可以在內置的數據集上演示這個,但是如果不是,你應該用'dput()'或者通過模擬來分享你的數據。 [請參閱這裏瞭解製作可重複示例的技巧](http://stackoverflow.com/q/5963269/903061)。 – Gregor
此外,它很好,如果例子*最小* - 有助於消除分心的東西,從實際問題。如果自定義顏色,標籤,角度文本,主題自定義等不屬於問題的一部分,請將其從您的問題中刪除。 – Gregor
通過使用expand = c(0,0),可以將y軸的極限值設置爲數據集中最大和最小y值。如果你的錯誤條可能非常大以至於它們低於零(奇怪但可能),那麼最小值就是這個值(例如-1),而不是零。也許嘗試並添加,限制= c(0,0)scale_y_continuous – Wave