This is a rather basic question, but I would like to double check something.
I have a counts of an item for the years 1970 to 1999. For some years, I have no values (implicit NAs). The yearly counts have grouped into five years intervals. The 5 year intervals are the facets in my ggplot.
When I produce the plot I use scales="free_x"
with the intent do get the x breaks only for the years covered by the relevant facet. My problem now is that the missing years are not displayed. What I would like to have is all 5 relevant years/breaks displayed in each facet, not only those for which data are available.
Here is my question: I am correct that I can remedy this issue only by making the missing years explicit, ie. creating 0 or NA values for each missing year; or by creating ordered factors and printing all their levels on the x axis?
I am just intrigued that there is no more straightforward approach with continuous scales, particularly when I have anyway a vector defining all the breaks.
Below is my example. Many thanks.
library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 4.2.1
#> Warning: package 'tibble' was built under R version 4.2.1
df_data <- data.frame(
year = c(1971L,1972L,1973L,1976L,1977L,1979L,
1980L,1981L,1982L,1983L,1989L,1990L,1991L,1992L,1993L,
1994L,1995L,1996L,1998L,1999L),
value = c(2L,3L,4L,7L,8L,10L,11L,12L,13L,14L,
20L,21L,22L,23L,24L,25L,26L,27L,29L,30L)
)
df_data <- df_data %>%
mutate(year_5=cut(year, breaks=c(Inf, seq(1970, 1999, 5)), right=T))
class(df_data$year)
#> [1] "integer"
df_data %>%
ggplot()
geom_bar(aes(x=year, y=value),
stat="identity")
facet_wrap(vars(year_5))
df_data %>%
ggplot()
geom_bar(aes(x=year, y=value),
stat="identity")
scale_x_continuous(breaks=seq(1970,1999,1))
facet_wrap(vars(year_5),
scales="free_x")