我想创建一个刻面图,使用geom_vline()添加垂直线,并使用Annotate()标记它。对于情节的X轴,我想保持刻面的尺度免费,只显示da ...

问题描述 投票:0回答:0
annotate()

标记。对于图的X轴,我想保持刻面的尺度免费,并且仅显示数据框架各组的数据中存在的日期。 当facet和使用

+ geom_vline()
ggplot行为时,并且不会在未显示日期的垂直线中呈现垂直线,但是当我包含注释时,ggplot似乎扩大了该方面的x轴在
annotate()
中。 您知道一种避免这种方法的方法,并且仅在数据中真正包含坐标的方面呈现注释?
there是我问题的重复:
library(tidyverse)
library(ggplot2)

data <- data.frame(
  date = c(seq.Date(from = as.Date("2023-01-01"), by = "month", length.out = 12),
           seq.Date(from = as.Date("2023-07-01"), by = "month", length.out = 12)),
  value = runif(24, 10, 100),
  group = rep(c("A", "B"), each = 12)
)

# Date for the vertical line
vline_date <- as.Date("2023-04-01")

# Create the plot
p <- ggplot(data, aes(x = date, y = value)) +
  geom_line() +
  facet_wrap(~group, scales = "free_x") +
  geom_vline(aes(xintercept = vline_date)) +
  theme_minimal()

播放,没有表现良好 this this创建图表,但缺少注释。 如果添加注释,则结果将更改B组的X尺度。

annotate()

用一个方面绘制了现有的X尺度的播放

我必须在发布过程中承认,我在建议的问题中找到了解决方案。但是,由于问题的问题并未直接涵盖覆盖现有日期范围的描述的行为,因此我认为发帖仍然很有用。
我通过使用joran's

来获得我想要的东西,以创建一个仅覆盖存在日期的组然后使用

p2 <- ggplot(data, aes(x = date, y = value)) + geom_line() + facet_wrap(~group, scales = "free_x") + geom_vline(aes(xintercept = vline_date)) + annotate("text", x = as.Date(vline_date), y = 0.1, label = "important date", vjust = -1, color = "red", angle = 90, size = 4) + theme_minimal()

而不是平等地对待每个方面的数据框的解决方案。
annotate()
解决方案的geom_text!

	
r ggplot2 facet-wrap
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.