2017-05-14 32 views
2

如何刪除地塊周圍的所有空間?R ggplot:刪除地塊周圍的所有空間

的樣本數據

dfr <- data.frame(x=factor(1:5),y=c(3,5,4,2,4)) 

ggplot(dfr,aes(x,y))+ 
    geom_bar(stat="identity") 

我嘗試如下,但仍有左和底部的空間。 Bg邊框顏色添加到可視化邊緣。

ggplot(dfr,aes(x,y))+ 
    geom_bar(stat="identity")+ 
    labs(x="",y="")+ 
    theme(axis.ticks=element_blank(), 
     axis.text=element_blank(), 
     plot.background=element_rect(colour="steelblue"), 
     plot.margin=grid::unit(c(0,0,0,0),"cm")) 

enter image description here

回答

3

您需要添加axis.title=element_blank()到您的theme聲明。

enter image description here

+2

留下一小塊空間。刻度標記已被設置爲'element_blank()',但刻度標記的空間依然存在。將'axis.ticks.length = unit(0,「pt」)'添加到'theme'。檢查:'g = ggplotGrob(p); G $的寬度; g $ heights' –

+1

我最近學到的另一個技巧是使用'+ = labs(x = NULL,y = NULL)'''='「,y =」「'。 – Brian