2017-04-16 14 views
1

My圖表看起來是這樣的:當x值是類型因子時,如何設置多線圖(ggplot)繪圖區域的限制?

graph

我使用的代碼是這樣的

graph_substrat <- ggplot(substrat_long, aes(x=substrat,y=value, 
+ group=variable, linetype=variable)) + geom_line() + ylab("Suitability Index") 
+ xlab("Substrat") + background_grid(major = "xy", minor = "none") 

graph_substrat <- graph_substrat + theme(legend.position="none") 

我只是想繪製卵石和沙子之間線,和自由刪除空間在圖的開頭和結尾,以便這些行從Y軸開始(或至少非常接近它,並以X軸上的最後一個刻度結束)。

我試過limits,expandcoord_cartesian,但它沒有奏效。

請注意,X的類型爲factor而不是numeric

回答

1

包括expand = c(0,0)scale_x_discrete中描述的here

比較該地塊:

library(ggplot2) 
ggplot(iris, aes(x = Species, y = Petal.Length)) + 
    stat_summary(fun.y = "mean", geom = "bar") 

enter image description here

到了一個與expand = c(0,0)

ggplot(iris, aes(x = Species, y = Petal.Length)) + 
    stat_summary(fun.y = "mean", geom = "bar") + 
    scale_x_discrete(expand = c(0,0)) 

enter image description here

+0

謝謝!我試圖用'expand = c(0,0)'來解決它,但是我做了錯誤的美學。 'scale_x_discrete(expand = c(0,0))'訣竅! –