2016-06-10 86 views
1

如何擺脫藍色線條所在的所有空間?控制一個barplot的總寬度

數據:

data = data.frame(is_repeat = c(0,0,0,1,1,1,1,1,1,1), 
        value = c(12000,8000,20000,14000,15000,11000,20000,60000,20000, 20000)) 

data$is_repeat = factor(data$is_repeat, levels = c(0,1), 
             labels = c("One-time", "Repeat")) 

簡介:

ggplot(data, aes(is_repeat, value)) + 
     geom_bar(stat = "identity", width = 0.3) + 
     ggtitle("Title") + 
     xlab("Type of event") + 
     ylab("Total Value") + 
     ylim(0, 150000) + 
     theme_minimal() 

enter image description here

編輯:我看了看這個問題,並沒有解決我的問題。我的猜測是,在另一個問題的情節中,有4個小節,所以看起來很滿。我想減少X軸的總寬度。

另一編輯:添加數據。

+0

要獲得幫助,你一定會得給你的問題更多背景資料及解釋條之間添加空間。預期結果vs當前結果是什麼?你能展示更多的代碼嗎? – GaijinJim

+4

類似:http://stackoverflow.com/questions/25887066/remove-space-between-bars-ggplot2 – aosmith

+1

'geom_bar(stat =「identity」,width = 1)' – alistaire

回答

3

如果您想徹底刪除條之間的空間,你不介意酒吧的寬度,你可以用做:

geom_bar(stat="identity", position="stack", width=1) 

theme(aspect.ratio=1)

,並刪除空間從劇情到線的端點需要

scale_x_discrete(expand = c(0,0), limits=c("One-time", "Repeat")) 

所以,你的代碼如下所示:

ggplot(data, aes(is_repeat, value)) + 
    geom_bar(stat="identity", position="stack", width=1) + 
    ggtitle("Title") + 
    xlab("Type of event") + 
    ylab("Total Value") + 
    ylim(0, 150000) + 
    scale_x_discrete(expand = c(0,0), limits=c("One-time", "Repeat")) + 
    theme_minimal() 

和輸出:

enter image description here

你可以改變width=1