Building data time series in ggplot2 using facet_wrap

I am trying to build ts data divided by year for comparison.

The problem is that I cannot figure out how to get ggplot to skip missing dates on each chart.

My data structure is as follows:

> head(pmWaw)
                 date         stacja pm25 pm10       season  year month
1 2014-01-01 00:00:00 MzWarNiepodKom  100  122 winter (DJF) 2014       1
2 2014-01-01 00:00:00 MzWarszUrsynow   63   68 winter (DJF) 2014       1
3 2014-01-01 00:00:00 MzWarTarKondra   NA   79 winter (DJF) 2014       1
4 2014-01-01 01:00:00 MzWarNiepodKom   69   77 winter (DJF) 2014       1
5 2014-01-01 01:00:00 MzWarszUrsynow   36   38 winter (DJF) 2014       1
6 2014-01-01 01:00:00 MzWarTarKondra   NA   90 winter (DJF) 2014       1
> tail(pmWaw)
                     date       stacja pm25 pm10       season  year month
52483 2015-12-30 22:00:00 MzWarAlNiepo   36   47 winter (DJF) 2015      12
52484 2015-12-30 22:00:00 MzWarKondrat   26   29 winter (DJF) 2015      12
52485 2015-12-30 22:00:00 MzWarWokalna   36   44 winter (DJF) 2015      12
52486 2015-12-30 23:00:00 MzWarAlNiepo   39   59 winter (DJF) 2015      12
52487 2015-12-30 23:00:00 MzWarKondrat   36   39 winter (DJF) 2015      12
52488 2015-12-30 23:00:00 MzWarWokalna   40   49 winter (DJF) 2015      12

The ggplot2 code I came up with:

pmWaw %>%
    ggplot(aes(x=date, y=pm25)) + 
    geom_jitter(alpha=0.5) +
    geom_smooth() +
    facet_wrap( ~ year)

The resulting plot has gaps in each year that I would like to remove, but cannot figure out how:

ggplot result

+4
source share
1 answer

Try scales = 'free_x'infacet_wrap

like this

pmWaw %>%
    ggplot(aes(x=date, y=pm25)) + 
    geom_jitter(alpha=0.5) +
    geom_smooth() +
    facet_wrap( ~ year, scales = "free_x")
+1
source

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


All Articles