2012-10-18 99 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描述了一个潜在的技术,你在找什么。