用 ggplot ggh4x 嵌套标签标记每隔一个刻度

问题描述 投票:0回答:1

我试图从 2 开始每隔一个刻度删除标签,当我将嵌套标签与

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

r ggplot2 label xticks ggh4x
1个回答
3
投票

你可以尝试这个(通过

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

© www.soinside.com 2019 - 2024. All rights reserved.