繁体   English   中英

ggplot2 binwidth在facet_wrap直方图中没有响应

[英]ggplot2 binwidth not responding in facet_wrap histogram plot

非常感谢有关如何使我的代码或binwidth行为的任何想法。 我的数据集包含每隔几个小时“4”和每天“24”收集的带时间戳的数据点。 我试图在左边绘制4小时堆积直方图,在右边绘制24小时堆积直方图。 因此,我希望右侧的binwidth比左侧binwidth宽6倍。 但是我用binwidth尝试的一切都没有用。 x轴数据,data3 $ dts似乎是连续的而不是离散的,但也许我做得不对。

关于数据的重要说明:在右侧绘制的数据,小时数= 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"

从pastebin中提取数据的代码:

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方式太窄:

在此输入图像描述

@ justin 链接到Hadley Wickham的帖子有答案,即在不同的图层中绘制左右图。

使用ggplot中的2个新geom_histogram行更新了正确绘制的代码:

库(ggplot2)库(scale)库(grid)库(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

箱子的宽度实际上是相同的。 不同之处在于,90天的地块中有更多的垃圾箱。

你可以通过在facet_wrap设置scales="free_y"来看到这facet_wrap

您还可以查看这篇文章 ,其中描述了一种可能的技术来完成您正在寻找的工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM