Ggplot2 binwidth not responding on facet_wrap histogram chart

It would take any thoughts on how to make my code or binary behave. My dataset contains time stamps collected every few hours "4", and daily "24". I am trying to build a 4-hour histogram with a histogram on the left and a 24-second histogram with a histogram on the right. So I want the bin width on the right to be 6x wider than the left bin width. But everything I tried with binwidth did not work. The x, data3 $ dts axis data seems continuous, not discrete, but maybe I'm not doing it right.

Important note about data: data that has charts on the right, clock = 24, has a value of dts, which is always an integer. The data on the left, the clock = 4 data, have non-integer 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" 

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 correct one-sided graphs with too narrow a layer of width:

enter image description here

+6
source share
2 answers

The @justin link to the post by Hadley Wickham has an answer that is plotting left and right in different layers.

Updated code that decomposes correctly with two new geom_histogram lines inside 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 chart: http://imgur.com/9j1Xz

+3
source

The boxes are actually the same width. The difference is that there are still many bunkers on 90-day charts.

This can be seen by setting scales="free_y" in facet_wrap

You can also take a look at this post , which describes a potential technique for doing what you are looking for.

+1
source

Source: https://habr.com/ru/post/928081/


All Articles