ggplot2:当日期是一个因素时,使点与实际日期对齐

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

我制作了一个具有不连续x轴的时间序列,但是,我只知道当x轴

Date
是一个因素时如何制作这些类型的图。这导致的问题是,图中的点与表明这些点是在当月第一天收集的因素一致,但事实并非如此。有什么方法可以将点与实际的
Date
对齐,同时保持不连续的x轴?

示例

library(tidyverse)
library(grid)

set.seed(333)

# Create example dataset
df <- data.frame(
  Group = c('A','A','A','A','A','A','B','B','B','B','B','B'),
  Date = rep(c('2021-07-13','2021-08-22','2021-09-04','2022-06-20','2022-07-08','2022-08-25'), 2),
  Value = round(rnorm(12,50,20)))

# Do some data wrangling
df <- df %>%
  mutate(Date = as.Date(Date),
         year = year(Date),
         mon = month(Date),
         mon_abb = month.abb[mon],
         monYear = paste(mon_abb,year, sep = ' ')) %>%
  select(!c(mon,year,mon_abb))

# Will need the month abbreviation
sample_dates <- unique(df$monYear)

# Make figure
# Note: the points align on the month but I would like them to
# align with the actual calendar Date (i.e., all points would move
# to the right because none were collected on the 1st of the month)
ggplot(data = df, aes(x = factor(Date), y = Value, group = Group)) +
  geom_point(aes(fill = Group), size = 3, shape = 21, color = "black") +
  scale_x_discrete(labels = substr(sample_dates, 1, 3)) + # Pulls the month abb. for x-axis label
  xlab("") +
  theme_bw() +
  theme(plot.margin = unit(c(1, 1, 2, 1), "lines"),
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
        legend.position = 'right',
        panel.border = element_blank()) +
  guides(fill = guide_legend(nrow = 2)) +
  coord_cartesian(clip = 'off', ylim = c(0, 100)) +
  annotation_custom(rectGrob(gp = gpar(fill = NA))) +
  annotate(geom = "text", x = 2, y = -16, label = 2021, size = 4.5) +
  annotate(geom = "text", x = 5, y = -16, label = 2022, size = 4.5) +
  annotate(geom = 'rect', xmin = 3.4, xmax = 3.6, ymin = -10, ymax = -3, fill = 'white') +
  annotate(geom = 'segment', x = c(3.4,3.6), xend = c(3.4,3.6), y = -8, yend = -3)

创建于 2024-04-04,使用 reprex v2.0.2

r ggplot2
1个回答
0
投票

您可以使用

facet_grid(scales = "free_x", space = "free_x")
执行此操作,以便点在各个面上按比例间隔。

ggplot(data = df |> mutate(era = +(Date > as.Date("2022-06-01"))), 
       aes(x = Date, y = Value, group = Group)) +
  geom_point(aes(fill = Group), size = 3, shape = 21, color = "black") +
  scale_x_date(date_breaks = "1 month", date_labels = "%b\n%Y", 
               minor_breaks = NULL, expand = expansion(add = 31)) +
  # scale_x_discrete(labels = substr(sample_dates, 1, 3)) + # Pulls the month abb. for x-axis label
  xlab("") +
  theme_bw() +
  theme(plot.margin = unit(c(1, 1, 2, 1), "lines"),
        panel.spacing.x = unit(2, "lines"),
        strip.background = element_blank(),
        strip.text.x = element_blank(),
        axis.text.x = element_text(angle = 0, vjust = 0.5, hjust = 0),
        legend.position = 'right',
        panel.border = element_blank()) +
  guides(fill = guide_legend(nrow = 2)) +
  facet_grid(.~era, scales = "free_x", space = "free_x") +
  coord_cartesian(clip = 'off', ylim = c(0, 100)) 

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