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

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

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

  1. library(tidyr)
  2. library(dplyr)
  3. library(ggplot2)
  4. library(ggh4x)
  5. datz <- structure(list(dnum = c(19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
  6. 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
  7. 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
  8. 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L), value = c(1, 0, 2,
  9. 0, 0, 2, 0, 0, 1, 0, 1, 2, 3, 70, 127, 76, 71, 45, 37, 32, 30,
  10. 24, 18, 15, 6, 13, 6, 8, 6, 5, 2, 3, 0, 0, 2, 3, 0, 0, 2, 0,
  11. 2, 1, 0), mon = c("AUG", "AUG", "AUG", "AUG", "AUG", "AUG", "AUG",
  12. "AUG", "AUG", "AUG", "AUG", "AUG", "AUG", "SEP", "SEP", "SEP",
  13. "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP",
  14. "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP",
  15. "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP"
  16. )), class = "data.frame", row.names = c(NA, -43L))
  17. ggplot(data=datz,mapping = aes(x = interaction(dnum,mon),y = value)) +geom_bar(stat = "identity",width = 0.25)+
  18. scale_x_discrete(guide = "axis_nested") + theme_classic() + scale_y_continuous(expand = c(0,0),breaks = seq(0,150,10))+
  19. theme(panel.grid = element_blank(),
  20. panel.grid.major.y = element_blank(),
  21. axis.title.x = element_blank())

xwbd5t1u

xwbd5t1u1#

您可以尝试此操作(按interaction(datz$dnum,datz$mon)[c(T,F)]设置偶数观测中断):

  1. ggplot(data=datz,mapping = aes(x = interaction(dnum,mon),y = value)) +
  2. geom_bar(stat = "identity",width = 0.25)+
  3. scale_x_discrete(guide = "axis_nested",breaks = interaction(datz$dnum,datz$mon)[c(F,T)]) +
  4. theme_classic() +
  5. scale_y_continuous(expand = c(0,0),breaks = seq(0,150,10))+
  6. theme(panel.grid = element_blank(),
  7. panel.grid.major.y = element_blank(),
  8. axis.title.x = element_blank())

更复杂的样本

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

  1. ggplot(data=datz,mapping = aes(x = interaction(dnum,mon),y = value)) +
  2. geom_bar(stat = "identity",width = 0.25)+
  3. scale_x_discrete(guide = "axis_nested",
  4. breaks = interaction(datz$dnum[datz$dnum %% 2 == 0] ,datz$mon[datz$dnum %% 2 == 0])) +
  5. theme_classic() +
  6. scale_y_continuous(expand = c(0,0),breaks = seq(0,150,10))+
  7. theme(panel.grid = element_blank(),
  8. panel.grid.major.y = element_blank(),
  9. axis.title.x = element_blank())

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

第三版

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

  1. ggplot(data=datz,mapping = aes(x = interaction(dnum,mon),y = value)) +
  2. geom_bar(stat = "identity",width = 0.25)+
  3. scale_x_discrete(guide = "axis_nested",
  4. breaks = interaction(datz$dnum, datz$mon), # you can remove this line (in this case) or use it to specify user defined ticks vector
  5. labels = datz %>% # and set labels only
  6. rowwise() %>%
  7. transmute(brl = paste0(ifelse(dnum %% 2 == 0, dnum, " "), ".", mon)) %>%
  8. pull
  9. ) +
  10. theme_classic() +
  11. scale_y_continuous(expand = c(0,0),breaks = seq(0,150,10))+
  12. theme(panel.grid = element_blank(),
  13. panel.grid.major.y = element_blank(),
  14. axis.title.x = element_blank())

展开查看全部

相关问题