R语言 使用ggplot ggh4x嵌套标签每隔一个标记

laawzig2  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(112)

我试图从2开始每隔一个tick删除标签,当我使用ggh4x包的嵌套标签时,我无法做到这一点。
我的第二个问题:有没有办法将“AUG”和“SEP”向左移动(左对齐),并在日期和mon(“AUG”,“SEP”)变量之间添加更多的空间?

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())

xwbd5t1u

xwbd5t1u1#

您可以尝试此操作(按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())

更复杂的样本

如果你需要严格的甚至日期,尽管在一个月的天数计数,你可以使用这个:

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())

在这里,我们有两个连续的日子跳过8月31日和9月1日

第三版

如果我们需要添加奇数刻度并将月份栏扩展到月份的偶数第一天和最后一天(8月31日和9月1日),我们可以这样做:

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())

相关问题