【问题标题】:ggplot2 binwidth not responding in facet_wrap histogram plotggplot2 binwidth 在 facet_wrap 直方图中没有响应
【发布时间】:2012-10-09 12:46:15
【问题描述】:

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

关于数据的重要说明:在右侧绘制的数据,即 hours=24 数据,具有始终为整数的 dts 值。左边的数据,hours=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 太窄:

【问题讨论】:

  • 如果你问我这些箱子的宽度是一样的。不同之处在于 90 天的地块有大约 90/7 倍的箱子要显示! (您可以在 facet_wrap 中使用 scales='free_y' 来查看。
  • 贾斯汀谢谢。你说得对,垃圾箱的宽度是一样的,但这就是我想要解决的问题。右侧图表应具有覆盖全天的 bin 宽度,例如比左图宽 6 倍。另外,我已经在使用包含 scales=free_x 和 scales=free_y 的 scales="free" ?
  • scales="free' 让两者都不同,但为了说明我的观点,我限制了 x 比例以匹配所有四个图。 here 是关于这个和 Hadley 解决方案的 ggplot 讨论的旧链接(但最近情况可能发生了变化)
  • @justin,太好了,谢谢。哈德利的解决方案奏效了!从他的帖子/您的链接中,相关信息是:` ggplot(dat=dat, aes(x=val, y=..ncount..)) + geom_histogram(data = subset(data, label == "a") , binwidth=1/50) + geom_histogram(data = subset(data, label == "b"), binwidth=1/10) `
  • @Justin 您能否发表您的评论作为解决方案,以便将此问题标记为已回答?

标签: r ggplot2


【解决方案1】:

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

您可以通过在facet_wrap 中设置scales="free_y" 来查看此内容

您还可以查看this post,它描述了一种潜在的技术来做您正在寻找的事情。

【讨论】:

    【解决方案2】:

    @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
    

    校正图:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      • 2012-12-21
      • 2019-03-12
      • 2021-08-04
      相关资源
      最近更新 更多