2012-10-18 35 views
6

希望瞭解如何讓我的代碼或binwidth行爲的任何想法。我的數據集包含每隔幾小時「4」和每日「24」收集時間戳數據點。我試圖在左側繪製4小時堆積的直方圖,在右側繪製24小時堆積的直方圖。因此,我希望右邊的binwidth比左邊的binwidth寬6倍。但是我用binwidth嘗試過的所有東西都沒有奏效。 x軸數據data3 $ dts看起來是連續的,不是離散的,但也許我沒有那麼做。ggplot2 binwidth在facet_wrap直方圖中沒有響應

有關數據的重要說明:在右側繪製數據的小時數= 24的數據具有始終爲整數的dts值。左邊的數據,小時= 4數據,具有非整數dts值。

   "dts" "Yes" "No" "Maybe" "hours" "days" 
"258" 15627.668 8  0 1  4 "7 Days" 
"259" 15627.832 13  11 18  4 "7 Days" 
"260" 15628  34  47 89  4 "7 Days" 
"261" 15628  37  47 90  24 "7 Days" 
"262" 15628.168 3  0 1  4 "7 Days" 
"40" 15571  345  419 674  24 "90 Days" 
"41" 15571.5  91  145 130  4 "90 Days" 
"42" 15571.668 158  149 284  4 "90 Days" 
"43" 15571.832 96  125 260  4 "90 Days" 
"44" 15572  55  33 137  4 "90 Days" 
"45" 15572  1050 1119 2660 24 "90 Days" 

代碼與數據從引擎收錄拉:

library (ggplot2) 
library (scales) 
library(grid) 
library(gridExtra) 

color3 <- c("mediumspringgreen","red","grey44") 
titles.days <- c("7 Days", "90 Days") 
names.facetby <- c ("dts", "hours", "days") 

data3 <- read.table ("http://pastebin.com/download.php?i=wUQQUXP4", header=TRUE) 
data3.melt <- melt (data3 , id = names.facetby) 
data3.melt$days <- factor (data3.melt$days, levels = titles.days) # put the factor in the right order, so the graphs are in the right order 

a <- ggplot  (data3.melt 
     , aes (  x = dts #as.Date(dts , date1970) 
       , y = value 
       , fill = variable)) + 
     opts (axis.text.x=theme_text(angle=0, hjust=1)) + 
     scale_fill_manual(values = color3) + 
     scale_x_date(labels = date_format("%m/%d\n %a")) + 
     geom_histogram (stat = "identity", position = "stack", binwidth=6.2) + 
     facet_wrap(days ~ hours, ncol=2, scales="free")    

print(a)   

目前的成績,呈現出binwidth路權雙面圖表太窄:

enter image description here

+1

如果你問我的箱子是相同的寬度。不同的是,90天的地塊有大約90/7倍的箱子可供展示! (你可以在你的'facet_wrap'中使用'scales ='free_y''來看到這個 – Justin

+0

Justin thanks。你說得對,箱子寬度相同,但這正是我想要解決的問題。我已經在使用scale =「free」,其中包含scale = free_x和scales = free_y? – hhk

+1

'scales =「free」這兩個圖表應該包含整個一天的bin寬度,例如比左圖寬6倍。 '讓兩者都有所不同,但爲了說明我的觀點,我限制了所有四個地塊的'x'比例。[這裏](https://groups.google.com/forum/?fromgroups=#!topic/ggplot2/ aQQ2hTYRQF8)是ggplot討論這個問題和Hadley解決方案的舊鏈接(但是最近有可能發生變化) – Justin

回答

3

@ justin的link to Hadley Wickham's post有答案,即在不同圖層中繪製左右圖。

更新代碼與所述ggplot 2條內新geom_histogram線正確地繪製:

庫(GGPLOT2) 庫(鱗) 庫(網格) 庫(gridExtra)

color3 <- c("mediumspringgreen","red","grey44") 
titles.days <- c("7 Days", "90 Days") 
names.facetby <- c ("dts", "hours", "days") 

data3 <- read.table ("http://pastebin.com/download.php?i=wUQQUXP4", header=TRUE) 
data3.melt <- melt (data3 , id = names.facetby) 
data3.melt$days <- factor (data3.melt$days, levels = titles.days) # put the factor in the right order, so the graphs are in the right order 



a <- ggplot  (data3.melt 
     , aes (  x = dts #as.Date(dts , date1970) 
       , y = value 
       , fill = variable)) + 
     opts (axis.text.x=theme_text(angle=0, hjust=1)) + 
     scale_fill_manual(values = color3) + 
     scale_x_date(labels = date_format("%m/%d\n%a")) + 

    # bad idea, good ideas follow geom_histogram (stat = "identity", position = "stack", binwidth=6.2) + #, breaks = breaks.x 
     geom_histogram (data = subset(data3.melt, hours == 4), stat = "identity", position = "stack", binwidth=0.3) + #, breaks = breaks.x 
     geom_histogram (data = subset(data3.melt, hours == 24), stat = "identity", position = "stack", binwidth=0.9) + #, breaks = breaks.x 

     facet_wrap(days ~ hours, ncol=2, scales="free")    

print(a)  # plot the thing 

更正圖表: http://imgur.com/9j1Xz

1

箱櫃實際上是相同的寬度。不同之處在於90天的地塊有更多的垃圾箱。

您可以通過在facet_wrap

設置scales="free_y"看到這一點,你也可以看看this post描述了一個潛在的技術,你在找什麼。