2012-07-05 516 views
9

所以我試圖做一個條形寬度映射到變量的堆積條形圖;但我想我的酒吧之間的間距是恆定的。ggplot2中的條形圖,寬度作爲一個變量,甚至條間的間距

有沒有人知道如何使間隔常數之間的酒吧?

現在我有這樣的:提前

p<-ggplot(dd, aes(variable, value.y, fill=Date, width=value.x/15))+ coord_flip() + opts(ylab="") 

p1<-p+ geom_bar(stat="identity") + scale_fill_brewer(palette="Dark2") + scale_fill_hue(l=55,c=55) 

p2<-p1 + opts(axis.title.x = theme_blank(), axis.title.y = theme_blank()) 

p2 

enter image description here

感謝。

順便說這是我的數據(抱歉長,體積大dput):

> dput(dd) 
structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 3L, 
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 7L, 7L, 7L, 7L, 7L, 2L, 2L, 
2L, 2L, 2L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L, 5L, 9L, 9L, 9L, 
9L, 9L, 8L, 8L, 8L, 8L, 8L), .Label = c("Alcohol and Tobacco", 
"Health and Personal Care", "Clothing", "Energy", "Recreation and Education", 
"Household", "Food", "Transportation", "Shelter"), class = "factor", scores = structure(c(2.91, 
5.31, 10.08, 15.99, 4.95, 11.55, 11.2, 27.49, 20.6), .Dim = 9L, .Dimnames = list(
    c("Alcohol and Tobacco", "Clothing", "Energy", "Food", "Health and Personal Care", 
    "Household", "Recreation and Education", "Shelter", "Transportation" 
    )))), value.x = c(2.91, 2.91, 2.91, 2.91, 2.91, 5.31, 5.31, 
5.31, 5.31, 5.31, 10.08, 10.08, 10.08, 10.08, 10.08, 15.99, 15.99, 
15.99, 15.99, 15.99, 4.95, 4.95, 4.95, 4.95, 4.95, 11.55, 11.55, 
11.55, 11.55, 11.55, 11.2, 11.2, 11.2, 11.2, 11.2, 27.49, 27.49, 
27.49, 27.49, 27.49, 20.6, 20.6, 20.6, 20.6, 20.6), Date = structure(c(5L, 
4L, 3L, 2L, 1L, 5L, 4L, 3L, 2L, 1L, 5L, 4L, 3L, 2L, 1L, 5L, 4L, 
3L, 2L, 1L, 5L, 4L, 3L, 2L, 1L, 5L, 4L, 3L, 2L, 1L, 5L, 4L, 3L, 
2L, 1L, 5L, 4L, 3L, 2L, 1L, 5L, 4L, 3L, 2L, 1L), .Label = c("1993-2001", 
"2001-2006", "2007-2010", "2010-2011", "2012 Jan - May"), class = "factor"), 
    value.y = c(2.1, 2.5, 7.6, 21.7, 2.8, 1.5, 0.3, -4.1, -4.2, 
    4.7, 3, 16.9, 1.9, 32.8, 23.9, 3.2, 4.6, 11.3, 8.9, 12.9, 
    1.7, 2, 7.8, 5.9, 10, 1.9, 2.1, 5.6, 2.2, 9.9, 1.4, 1.3, 
    2.2, 0.6, 17.3, 1.1, 2.3, 6.4, 13.1, 10, 4.3, 7.6, 0.9, 15.2, 
    20.5)), .Names = c("variable", "value.x", "Date", "value.y" 
), row.names = c(NA, -45L), class = "data.frame") 
+0

你可能要考慮使用+ facet_grid(〜Date)。它使得圖表更加可讀。 – 2012-07-05 16:32:11

回答

4

嘗試#2

我誘騙到GGPLOT2寫一個連續的尺度爲分類。

# The numbers for tmp I calculated by hand. Not sure how to program 
# this part but the math is 
# last + half(previous_width) + half(current_width) 
# Change the 1st number in cumsum to adjust the between category width 

tmp <- c(2.91,7.02,14.715,27.75,38.22,46.47,57.845,77.19,101.235) + cumsum(rep(5,9))

dd$x.pos1 <- rep(tmp,each=5) 
ggplot(dd,aes(x=x.pos1,y=value.y,fill=Date)) + 
geom_bar(aes(width=value.x),stat="identity",position="stack") + 
scale_x_continuous(breaks=tmp,labels=levels(dd$variable)) + 
coord_flip() 

enter image description here

良好的措施,你可能會想調整文字大小。這是用... + opts(axis.text.y=theme_text(size=12))

8

對於分類或「離散」規模 - 你可以調整寬度,但它需要0-1之間1.你的value.x超過1,因此重疊。您可以使用的重新調整,從規模封裝與重新調整以達到最佳「視圖中快速調整這使得杆的範疇內,寬度爲代表的一些其他變量(在這種情況下value.x)

install.packages("scales") 
library(scales) 
ggplot(dd,aes(x=variable,y=value.y,fill=Date)) + 
geom_bar(aes(width=rescale(value.x,c(0.5,1))),stat="identity",position="stack")' + 
coord_flip() 

播放「更改爲0.5至0.25 ...等

enter image description here

就個人而言,我覺得這樣的事情是提供更多的信息:

ggplot(dd,aes(x=variable,y=value.y,fill=Date)) + 
geom_bar(aes(width=rescale(value.x,c(0.2,1))),stat="identity") + 
coord_flip() + facet_grid(~Date) + opts(legend.position="none") 

enter image description here

+0

感謝您的回覆,但我實際上試圖通過重新調整條的寬度來停止重疊(爲了使它在視覺上有效,我需要大寬度差),我正在尋求強制酒吧之間的空白空間是恆定的。這可以做到嗎? – user1443010 2012-07-05 19:04:44

+0

另外,我看到在某些情況下,面網格是一個更好的選擇,但在這種情況下,這些年的價值達到了最終值,因此對於我的目的而言,原始格式是好的。 – user1443010 2012-07-05 19:07:47

+0

對於分類變量,我不認爲有一種方法可以針對每個尺度元素唯一地調整寬度。儘管在0.91中對scale_x_discrete()進行了一些調整,所以也許你的問題會在ggplot2郵件列表中得到更好的回答。就最好的看法而言,如果要將它們放在一起,但仍希望在類別之間進行比較,那麼在每個欄上添加標籤可能會有所幫助。 – 2012-07-05 19:31:28