2017-07-15 240 views
0

如何調整(增加或減少)軸文本(數字)和繪圖區域(灰色區域)之間的間距?ggplot2:如何調整繪圖區域和軸文本之間的間距

dfr <- data.frame(x=1:5,y=1:5) 
ggplot(dfr,aes(x,y))+ 
    geom_point()+ 
    theme(axis.title=element_blank(), 
     axis.ticks=element_blank()) 

fig

+0

[這個問題](https://stackoverflow.com/questions/22945651/how-to-remove-space-between-axis-area-plot-in-ggplot2)可能是很有幫助。 –

+3

請參見[增加y軸上文本和標題之間的距離](https://stackoverflow.com/questions/14487188/increase-distance-between-text-and-title-on-the-y-axis)。只需將'axis.title.y'替換爲'axis.text.y'即可。 – Henrik

+0

是的!這似乎是正確的解決方案。 – rmf

回答

1

一種選擇是使用axis.ticks.length()設置繪圖區和軸文本之間的空間,因爲你選擇不顯示蜱(axis.ticks=element_blank())。

ggplot(dfr,aes(x,y))+ 
    geom_point()+ 
    theme(axis.title=element_blank(), 
      axis.ticks.length = unit(.85, "cm"), 
      axis.ticks=element_blank()) 

它產生的輸出:

enter image description here

或者,也可以定義margin()的參數(T,R,B,L)到adjust the space

ggplot(dfr,aes(x,y))+ 
    geom_point()+ 
    theme(axis.title=element_blank(), 
     axis.ticks=element_blank(), 
     axis.text.x=element_text(margin = margin(t = 20)), 
     axis.text.y=element_text(margin = margin(r = 20))) 

enter image description here

+0

哈克解決方案!我很驚訝沒有適當的參數來調整這一點。 – rmf

+1

應該有一個適當的解決方案來做到這一點,但我無法輕鬆找到它們。希望聽到別人更好的解決方案! – Prradep

相關問題