首页 > 解决方案 > 与拼凑组合时,将 geom_point 数据与 geom_col 数据对齐

问题描述

我正在努力创建两个图形并使用拼凑将它们合并成一个图形。x 轴是日期,上图是 geom_line/geom_point 图,下图是使用 geom_col 创建的。组合时,我希望顶部图形中的点与底部图形中的列中心对齐。

我希望 b/c x 轴跨越相同的日期,这些点将与列的中心对齐。在几次尝试中,我无法将点放在柱子上方的中心。生成的图形(和代码)如下。

点不在列的中心

library(patchwork)


nn<-15
p1data = tibble(date=ymd('2021-01-01') + days(0:(nn-1)), values=floor(runif(nn, 5.0, 75)))

p2data <- tibble(initdate = ymd('2021-01-01')+days(0:(nn-1)), data=runif(nn,4,10))

# set up a tibble to shade in every other day
shade <- tibble(min=ymd('2021-01-01 00:00')+days(0:20),max=lead(min))
# Remove every other row
toDelete <- seq(1,dim(shade)[1],by=2)
shade <- shade[-toDelete,]

plot_limits = c(ymd('2021-01-01'),ymd('2021-01-16'))

# Orginal Attempt 
p1 <- p1data %>% 
  ggplot() + 
  geom_rect(data=shade, aes(xmin=min,xmax=max,ymin=-Inf,ymax=Inf),fill='grey80' ) + 
  geom_line(aes(x=date,y=values)) + 
  geom_point(aes(x=date,y=values),color='red',size=3) +
  scale_x_date(expand=c(0,0),
                   date_breaks = '1 days',
                   date_labels = '%b-%d',
                   limits=plot_limits) +
  labs(title='two plots combined') + 
  theme_bw() + 
  theme(plot.title=element_text(hjust=0.5), 
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.x=element_blank())
  

p2 <- p2data %>%  
  ggplot() + 
  geom_col(aes(x=initdate,y=data, fill=data)) + 
  scale_x_date(expand=c(0,0),date_breaks = '1 days',date_labels = '%b-%d') +
  theme_bw() + 
  theme(plot.title=element_blank(),
        axis.title.x=element_blank(),
        axis.text.x=element_text(angle=0),
        legend.direction = "horizontal", 
        legend.position = "bottom",
        legend.key.width=unit(1.25,"cm"),
        legend.key.height=unit(.25,"cm"),
        legend.box = "vertical",
        legend.box.background = element_blank(),
        legend.title = element_text(face = "bold"),
        legend.spacing = unit(1,"cm"),
        legend.justification = "center",
        legend.margin = margin(0.,0.,0.,0., unit="cm"),
        legend.background = element_blank(),
        plot.margin = margin(0, 0, 0, 0, "cm")
  )

p1/p2

是否有其他方法可以在顶部图表中将每一天的点与底部图表中的列居中?

标签: rggplot2

解决方案


在准备这个问题时,我能够开发出一个解决方案。解决方案是对顶部图形使用日期时间格式,而不仅仅是日期。然后可以将与数据关联的日期时间设置为 YYYY-MM-DD 12:00。然后在一天中而不是在每天开始时绘制数据。

生成的图形是: 在此处输入图像描述

实现它的代码是:

# The solution that works
# Specify the dates in datetime format and use the hours to center the data in the day. 

# The shade data for daily shades must be in datetime asd well.
# set up a tibble to shade in every other day
shade_dtm <- tibble(min=ymd_hm('2021-01-01 00:00')+days(0:20),max=as_datetime(lead(min)))
# Remove every other row
toDelete <- seq(1,dim(shade_dtm)[1],by=2)
shade_dtm <- shade_dtm[-toDelete,]

# create data
p1data_dtm = tibble(date=ymd_hm('2021-01-01 12:00') + days(0:(nn-1)), values=floor(runif(nn, 5.0, 75)))

plot_limits_dtm = c(ymd_hm('2021-01-01 00:00'),ymd_hm('2021-01-16 00:00'))

p1 <- p1data_dtm %>% 
      ggplot() + 
      geom_rect(data=shade_dtm, aes(xmin=min,xmax=max,ymin=-Inf,ymax=Inf),fill='grey80' ) + 
      geom_line(aes(x=date,y=values)) + 
      geom_point(aes(x=date,y=values),color='red',size=3) +
      scale_x_datetime(expand=c(0,0),
                       date_breaks = '1 days',
                       date_labels = '%b-%d',
                       limits=plot_limits_dtm) +
      labs(title='two plots combined: data aligned') + 
      theme_bw() + 
      theme(plot.title=element_text(hjust=0.5), 
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.x=element_blank())


p2 <- p2data %>%  
      ggplot() + 
      geom_col(aes(x=initdate,y=data, fill=data)) + 
      scale_x_date(expand=c(0,0),date_breaks = '1 days',date_labels = '%b-%d') +
      theme_bw() + 
      theme(plot.title=element_blank(),
        axis.title.x=element_blank(),
        axis.text.x=element_text(angle=0),
        legend.direction = "horizontal", 
        legend.position = "bottom",
        legend.key.width=unit(1.25,"cm"),
        legend.key.height=unit(.25,"cm"),
        legend.box = "vertical",
        legend.box.background = element_blank(),
        legend.title = element_text(face = "bold"),
        legend.spacing = unit(1,"cm"),
        legend.justification = "center",
        legend.margin = margin(0.,0.,0.,0., unit="cm"),
        legend.background = element_blank(),
        plot.margin = margin(0, 0, 0, 0, "cm")
  )

p1/p2

推荐阅读