【问题标题】:How to change the x-axis ticks in a facet-wrapt stacked bar chart--ggplot R如何更改刻面环绕堆积条形图中的 x 轴刻度--ggplot R
【发布时间】:2021-10-29 13:29:33
【问题描述】:

我正在尝试用我的数据框中的另一个变量替换多面堆积条形图中的 x 轴刻度。我正在比较两个不同年份的相应周,所以我在 x 轴上使用“年”变量,在 y 轴上使用“总”变量并用年份中的周数包装(即一月的第一周得到数字 1,第二个是 2 等等)。 我已经尝试了很多东西,包括 scale_x_discrete; scale_x_date; axi.ticks.x、geom_text 等......

这是一个可重现的例子:

dat <- data.frame(date = as.Date(c("2021-01-03", "2020-01-05", "2021-01-03", "2020-01-05", "2021-01-03", "2021-01-03", "2021-01-10","2020-01-12", "2021-01-10", "2020-01-12", "2021-01-10", "2021-01-10", "2021-01-17", "2020-01-19", "2021-01-17", "2020-01-19", "2021-01-17", "2021-01-17")),
                 total = c(109334, 150052, 36546, 51476, 2961, 19988, 106719, 126748, 31893, 
                           29576, 5401, 19336, 91021, 108892, 23030, 26115, 3861, 15663),
                 product = c("A", "A","B", "B", "C", "D", "A", "A", "B", "B", "C", "D", "A", 
                              "A","B", "B", "C", "D"),
                 week = c(1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3),
                 year = c(2021,2020,2021,2020,2021,2021,2021,2020,2021,2020,2021,2021,2021,2020,2021,2020,2021,2021))  

dat %>% ggplot(aes(year, total, color = year == 2021)) +
  geom_bar(position = "stack", stat = "identity", size =0.6)+
  #scale_x_date(expand = c(0,0), labels = date_format("%m/%d/%y"))+
  scale_y_continuous(expand = c(0,0), breaks = seq(0, 350000, by = 50000), limits=c(0, 350000))+
  #scale_x_discrete(breaks = dat$year, labels = dat$date)+
  scale_color_manual(values = c(NA, 'red'), guide=F)+
  facet_wrap(~week, nrow = 1)+
  #geom_text(aes(y= total, label = date), position = position_dodge(width = 1), angle = 90, vjust = -1, size = 2)+
  theme(axis.text.x=element_text(angle = 90, vjust =0, hjust =0),
        #axis.ticks.x = ,
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        panel.background = element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.spacing.x = unit(0.1, "lines"))

最终结果将是 x 轴显示周日期(该周的星期日日期)。 desired x-axis ticks (please ignore the * on it)

谢谢!

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。
  • object 'interaction_id' not found
  • 我添加了进一步的解释,并更改了可重现的示例。所以你现在应该很好。谢谢!

标签: r date ggplot2 charts data-visualization


【解决方案1】:

看起来您可以在这里使用离散的 x 轴,因为您不关心时间轴上的日期位置,而只关心按时间顺序排列的类别。使用as.character(date) 可以实现这一点,或者您可以换入format(date, "%d-%b-%Y") 以复制示例中的日期格式。然后您还需要facet_wrapscales = "free_x" 参数,以便每个构面都可以仅显示出现在该构面中的日期。

编辑:如果要显示格式化日期,但要按时间顺序排列,则需要将格式化日期作为一个因素。 forcats::reorder() 提供了一种基于另一个变量来控制因子顺序的方法。

dat %>% 
    mutate(date_label = format(date, "%d-%b-%Y") %>% forcats::fct_reorder(date)) %>%
    ggplot(aes(date_label, total, color = year == 2021)) +
    geom_bar(position = "stack", stat = "identity", size =0.6)+
    scale_y_continuous(expand = c(0,0), breaks = seq(0, 350000, by = 50000), limits=c(0, 350000))+
    scale_color_manual(values = c(NA, 'red'), guide=F)+
    facet_wrap(~week, nrow = 1, scales = "free_x")+
    ...

【讨论】:

  • 嗨乔恩·斯普林!非常感谢,您的回答效果很好,为我的工作节省了很多额外的时间!
  • 顺便说一句,我添加了一个步骤以使标签成为有序因子。使用 as.character(date) 时没有必要这样做,因为 YYYY-MM-DD 的字母顺序和时间顺序都相同,但如果您使用不同的日期格式,ggplot 将按字母顺序显示类别,这可能不是您想要的.
  • 谢谢阿甘,太完美了