Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 binwidth not responding in facet_wrap histogram plot

Tags:

r

ggplot2

Would appreciate any thoughts on how to make my code or binwidth behave. My dataset contains timestamped data points collected every few hours "4", and daily "24". I am trying to plot the 4 hour stacked histograms on the left and the 24 hour stacked histograms on the right. Therefore, I want the binwidth on the right to be 6x wider than the left binwidth. But everything I've tried with binwidth has not worked. The x axis data, data3$dts appears to be continuous and not discrete but maybe I'm not doing that right.

Important note about the data: Data that gets graphed on the right side, the hours=24 data, has a dts value that is always an integer. Data on the left, the hours=4 data, has non-integer dts values.

             "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"

Code with data pulled from 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)        

Current results, showing the right sided graphs with binwidth way too narrow:

enter image description here

like image 932
hhk Avatar asked Oct 18 '12 23:10

hhk


1 Answers

@justin's link to Hadley Wickham's post has the answer, which is to plot left and right graphs in different layers.

Updated code that plots correctly with 2 new geom_histogram lines inside the ggplot:

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") ) +

    # 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

Corrected graph: http://imgur.com/9j1Xz

like image 158
hhk Avatar answered Sep 24 '22 05:09

hhk