I'm trying to remove labels every other tick starting from 2, I'm unable to do this when I use nested labels with ggh4x
package.
My second question: Is there a way to move "AUG" and "SEP" to the left (align left) and add more space between the days and the mon ("AUG", "SEP") variable?
library(tidyr)
library(dplyr)
library(ggplot2)
library(ggh4x)
datz <- structure(list(dnum = c(19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L), value = c(1, 0, 2,
0, 0, 2, 0, 0, 1, 0, 1, 2, 3, 70, 127, 76, 71, 45, 37, 32, 30,
24, 18, 15, 6, 13, 6, 8, 6, 5, 2, 3, 0, 0, 2, 3, 0, 0, 2, 0,
2, 1, 0), mon = c("AUG", "AUG", "AUG", "AUG", "AUG", "AUG", "AUG",
"AUG", "AUG", "AUG", "AUG", "AUG", "AUG", "SEP", "SEP", "SEP",
"SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP",
"SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP",
"SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP"
)), class = "data.frame", row.names = c(NA, -43L))
ggplot(data=datz,mapping = aes(x = interaction(dnum,mon),y = value)) +geom_bar(stat = "identity",width = 0.25)+
scale_x_discrete(guide = "axis_nested") + theme_classic() + scale_y_continuous(expand = c(0,0),breaks = seq(0,150,10))+
theme(panel.grid = element_blank(),
panel.grid.major.y = element_blank(),
axis.title.x = element_blank())
You may try this (set breaks on even observations by interaction(datz$dnum,datz$mon)[c(T,F)]
):
ggplot(data=datz,mapping = aes(x = interaction(dnum,mon),y = value)) +
geom_bar(stat = "identity",width = 0.25)+
scale_x_discrete(guide = "axis_nested",breaks = interaction(datz$dnum,datz$mon)[c(F,T)]) +
theme_classic() +
scale_y_continuous(expand = c(0,0),breaks = seq(0,150,10))+
theme(panel.grid = element_blank(),
panel.grid.major.y = element_blank(),
axis.title.x = element_blank())
A more complicated sample
If you need strictly even dates despite of days count in a monthh you can use this:
ggplot(data=datz,mapping = aes(x = interaction(dnum,mon),y = value)) +
geom_bar(stat = "identity",width = 0.25)+
scale_x_discrete(guide = "axis_nested",
breaks = interaction(datz$dnum[datz$dnum %% 2 == 0] ,datz$mon[datz$dnum %% 2 == 0])) +
theme_classic() +
scale_y_continuous(expand = c(0,0),breaks = seq(0,150,10))+
theme(panel.grid = element_blank(),
panel.grid.major.y = element_blank(),
axis.title.x = element_blank())
Here we have two consequent days skipped Aug., 31 and Sep., 1
3rd Edition
If we neen to add odd ticks and extend month bar to positions under even first and last days of the months (Aug., 31 and Sep., 1) we can do the following:
ggplot(data=datz,mapping = aes(x = interaction(dnum,mon),y = value)) +
geom_bar(stat = "identity",width = 0.25)+
scale_x_discrete(guide = "axis_nested",
breaks = interaction(datz$dnum, datz$mon), # you can remove this line (in this case) or use it to specify user defined ticks vector
labels = datz %>% # and set labels only
rowwise() %>%
transmute(brl = paste0(ifelse(dnum %% 2 == 0, dnum, " "), ".", mon)) %>%
pull
) +
theme_classic() +
scale_y_continuous(expand = c(0,0),breaks = seq(0,150,10))+
theme(panel.grid = element_blank(),
panel.grid.major.y = element_blank(),
axis.title.x = element_blank())