2017-04-25 83 views
0

請參見後續情節,例如:如何抑制x軸的擴展並防止裁剪軸標籤?

library(ggplot2) 

df <- head(mtcars, 4) 

df$rn <- rownames(df) 

#>     mpg cyl disp hp drat wt qsec vs am gear carb 
#> Mazda RX4  21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 
#> Datsun 710  22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 
#>       rn 
#> Mazda RX4   Mazda RX4 
#> Mazda RX4 Wag Mazda RX4 Wag 
#> Datsun 710   Datsun 710 
#> Hornet 4 Drive Hornet 4 Drive 

ggplot(df, aes(x = rn, y = mpg)) + 
    geom_point() + 
    scale_x_discrete(expand = c(0, 0)) 

enter image description here

我想與expand = c(0, 0)刪除灰色繪圖區多餘的區域,而被裁剪避免最後x軸標籤'Mazda RX4 Wag'。我怎樣才能做到這一點?

+0

@beetroot,感謝您的鏈接。這真的是我想要的。我會刪除這篇文章。 – mt1022

+0

@Axeman,在這裏留下一個重複的問題是否好?我真的不確定哪種方式更合適。 – mt1022

+0

嗯,我從這裏找到答案:https://meta.stackoverflow.com/questions/265736/should-i-delete-my-question-if-it-is-marked-as-a-duplicate – mt1022

回答

0

這是怎麼回事?

ggplot(df, aes(x = rn, y = mpg)) + 
    geom_point() + 
    scale_x_discrete(expand = c(0, 0))+ 
    coord_fixed(ratio = 1/0.40) 
+0

謝謝。 'coord_fixed'對我來說是一個新功能。但我更喜歡解決鏈接問題,這更加靈活。 – mt1022