title: Bar plot with variable bar widths as date ranges on the x-axis
body: <p>I wish to make a bar graph where the response variable (weight change) is measured over time periods of different length, defined by a start and an end date. The width of the bars should correspond to the length of the period. A small example of my data:</p>
  
  <pre><code>wtchange.data &lt;- structure(list(start.date = structure(1:3, .Label = c("2015-04-01", 
      "2015-04-15", "2015-04-30"), class = "factor"), end.date = structure(1:3, .Label = c("2015-04-15", 
      "2015-04-30", "2015-05-30"), class = "factor"), wtchange = c(5L, 
      10L, 15L), se = c(1.2, 2.5, 0.8)), .Names = c("start.date", "end.date", 
      "wtchange", "se"), class = "data.frame", row.names = c(NA, -3L
      ))
  
  wtchange.data
  #   start.date   end.date wtchange  se
  # 1 2015-04-01 2015-04-15        5 1.2
  # 2 2015-04-15 2015-04-30       10 2.5
  # 3 2015-04-30 2015-05-30       15 0.8
  
  wtchange.data$start.date &lt;- as.Date(wtchange.data$start.date)
  wtchange.data$end.date &lt;- as.Date(wtchange.data$end.date)
  </code></pre>
  
  <p>Attempting to use <code>geom_bar</code>:</p>
  
  <pre><code>library(ggplot2)
  ggplot(wtchange.data, aes(x = start.date, y = wtchange)) +
    geom_bar(stat = "identity", color = "black") +
    geom_errorbar(aes(ymin = wtchange-se, ymax = wtchange+se), width = 1)
  </code></pre>
  
  <p>(not allowed >2 links with &lt;10 reputation, so can unfortunately not show the first plot)</p>
  
  <p>The main problem is that when aesthetics of the plot area are defined (<code>x = start.date, y = wtchange</code>), I can use only one variable (<em>start.date</em> in this example) for the x-axis, but I really need to somehow use both <em>start.date</em> and <em>end.date</em> to delimit bar widths corresponding to each period. The graph should look something like this (drawn in Paint):
  <a href="https://i.stack.imgur.com/0MmHq.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0MmHq.jpg" alt="enter image description here"></a></p>
  
  <p>A secondary problem is that the bars should touch without gaps, but I am not sure if it is even possible, given that the bars have to be of different widths, so you cannot set one bar width for all bars. Would it be possible to set width for each bar manually?</p>
  
  <hr>
  
  <p>Edit:
  Thank you Henrik for the links. I have made some further progress.
  I calculated date midpoints for centering the bars at:</p>
  
  <pre><code>wtchange.data$date.midpoint &lt;- wtchange.data$start.date +
  (wtchange.data$end.date - wtchange.data$start.date)/2
  </code></pre>
  
  <p>And then calculated period lengths for using as bar widths:</p>
  
  <pre><code>wtchange.data$period.length &lt;- wtchange.data$end.date - wtchange.data$start.date
  </code></pre>
  
  <p>The updated graph code is now:</p>
  
  <pre><code>ggplot(wtchange.data, aes(x = date.midpoint, y = wtchange)) +
    geom_bar(stat = "identity", color = "black", width = wtchange.data$period.length) +
    geom_errorbar(aes(ymin = wtchange-se, ymax = wtchange+se), width = 1)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/FBYdy.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FBYdy.jpg" alt="enter image description here"></a></p>
  
  <p>The only problem remaining is that there still is a small gap between bars in one place. I guess this is due to the way R rounds date difference calculation to the nearest number of days?</p>