ggplot,geom_bar, facet_wrap : How to define specific dates as x values, for each plot

Hi.
As image illustrates, my barplot are not symmetrical in terms of width of bars, due to different number of observations, and different timespans for the individual years (2015, 2016 and 2018). I want the bars to be of equal size, but defining the width (width=) it becomes very messy.

Besides, for some unknown reason, the dateformat changes in 2018, where i suddenly only have months on the x axis , and not months+day as the previous years.

I've tried everything i could think off, including the functions gap.plot, gap.barplot and axis.break from the plotrix package (v3.7-6), but it dosent work with my ggplot.

library(tidyverse)
library(rmarkdown)
library(readxl)
library(dplyr)
library(tidyr)
library(lubridate)
library(reshape2)
library (ggthemes)
library(ggpubr)
library(plotrix)
library(scales)

#printing dataframe
print(gapl151618_wetCS)

N_ug_m2_h   se_graph      Date      Trt  Year
<dbl>      <dbl>         <date>    <chr> <chr>
0.59172415	1.1529715	2018-06-15	C	2018
0.79847845	0.5764257	2018-07-01	C	2018
-2.02633456	-1.8041389	2018-09-19	C	2018
2.03877800	2.2450879	2018-06-15	S	2018
-1.19586851	-1.9191906	2018-07-01	S	2018
0.00776147	0.5356943	2018-09-19	S	2018
6.99500000	2.2524320	2016-07-07	C	2016
7.97983333	2.6705785	2016-07-18	C	2016
2.98933333	2.3721254	2016-08-01	C	2016
-1.65866667	-1.1795099	2016-08-15	C	2016
5.52157486	1.4964004	2015-08-06	C	2015
5.05983333	3.2670861	2016-07-07	S	2016
7.28733333	1.5233716	2016-07-18	S	2016
3.82466667	3.5809984	2016-08-01	S	2016
0.30750000	1.4447466	2016-08-15	S	2016
-0.09014181	-3.8432439	2015-08-06	S	2015

#creating plot
ggplot(gapl151618_wetCS, aes(x=Date , y=N_ug_m2_h, fill=Trt))+
  geom_hline (yintercept=0, color = "black", linetype = 3 ) +
  facet_wrap(~Year, scales = "free_x") +
  geom_bar(position = position_dodge2 (width = 1), stat = "identity", colour="black", size=.3) +
  geom_errorbar(aes(ymin=N_ug_m2_h, ymax=N_ug_m2_h+se_graph, group = Trt), position = position_dodge2 ()) +
   ylab(expression(~mu~g~N[2]*O-N~~m^{-2}~h^{-1})) +
  xlab("Date") +
  ggtitle(expression(N[2]*O~fluxes~wet~tundra~2015~","~2016~and~2018)) 


Defining width on geom_bar

theme_set(
  theme_bw()+
    theme(legend.position = "right",
          plot.title = element_text (hjust = 0.5, vjust = 1, size = 15),
          axis.text.x = element_text (angle = 60, hjust = 1)))

ggplot(gapl151618_wetCS, aes(x=Date , y=N_ug_m2_h, fill=Trt))+
  geom_hline (yintercept=0, color = "black", linetype = 3 ) +
  facet_wrap(~Year, scales = "free_x") +
  geom_bar(position = position_dodge2 (width = 1), stat = "identity", colour="black", size=.3, width = 1) +
  geom_errorbar(aes(ymin=N_ug_m2_h, ymax=N_ug_m2_h+se_graph, group = Trt), position = position_dodge2 ()) +
    ylab(expression(~mu~g~N[2]*O-N~~m^{-2}~h^{-1})) +
  xlab("Date") +
  ggtitle(expression(N[2]*O~fluxes~wet~tundra~2015~","~2016~and~2018)) 

If I correctly understand what you are trying to do, I think you can use factors on the x-axis instead of dates and use facet_grid() with space = "free" argument.

library(ggplot2)

gapl151618_wetCS <- data.frame(stringsAsFactors=FALSE,
   N_ug_m2_h = c(0.59172415, 0.79847845, -2.02633456, 2.038778, -1.19586851,
                 0.00776147, 6.995, 7.97983333, 2.98933333, -1.65866667,
                 5.52157486, 5.05983333, 7.28733333, 3.82466667, 0.3075, -0.09014181),
    se_graph = c(1.1529715, 0.5764257, -1.8041389, 2.2450879, -1.9191906,
                 0.5356943, 2.252432, 2.6705785, 2.3721254, -1.1795099,
                 1.4964004, 3.2670861, 1.5233716, 3.5809984, 1.4447466, -3.8432439),
        Date = as.Date(c("2018-06-15", "2018-07-01", "2018-09-19", "2018-06-15",
                 "2018-07-01", "2018-09-19", "2016-07-07", "2016-07-18",
                 "2016-08-01", "2016-08-15", "2015-08-06", "2016-07-07", "2016-07-18",
                 "2016-08-01", "2016-08-15", "2015-08-06")),
         Trt = c("C", "C", "C", "S", "S", "S", "C", "C", "C", "C", "C", "S",
                 "S", "S", "S", "S"),
        Year = c(2018, 2018, 2018, 2018, 2018, 2018, 2016, 2016, 2016, 2016,
                 2015, 2016, 2016, 2016, 2016, 2015)
)

ggplot(gapl151618_wetCS, aes(x= as.factor(Date), y=N_ug_m2_h, fill=Trt))+
    geom_hline (yintercept=0, color = "black", linetype = 3 ) +
    facet_grid(~Year, scales="free", space = "free") +
    geom_bar(position = position_dodge2 (width = 1), stat = "identity", colour="black", size=.3) +
    geom_errorbar(aes(ymin=N_ug_m2_h, ymax=N_ug_m2_h+se_graph, group = Trt), position = position_dodge2 ()) +
    ylab(expression(~mu~g~N[2]*O-N~~m^{-2}~h^{-1})) +
    xlab("Date") +
    ggtitle(expression(N[2]*O~fluxes~wet~tundra~2015~","~2016~and~2018))

Created on 2019-10-15 by the reprex package (v0.3.0.9000)

Note: Next time, please try to narrow down your code to just the relevant part and provide a proper REPRoducible EXample (reprex)

1 Like

Interesting that facet_grid has this option but not facet_wrap, I was looking into the help to find something like this.

Its a very nice suggestion, many thanks :slight_smile:
The problem is, that I still would like to have unequal spacing between bars on x axis (in order to visualize the different time-spans between the sets of observations (C and S), and this feature is lost in this solution. But it is still better than what i made so far:)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.