簡體   English   中英

在標題中使用兩個日期變量的文本的 R ggplot 變量

[英]R ggplot variable for text using two date variables in caption

我有一個項目,我試圖每周更新。

我想在項目頂部有日期范圍變量,我將用於創建不同表的不同過濾器。

我還希望這些變量通知 ggplot 中的標題。 有沒有辦法不只取兩個不同的變量字符串,並將它們都放入 ggplot 標題中?

您可以在圖表底部看到問題,上面寫着:“date_range_begin date_range_end 的周”

我有點覺得這對於擁有更多真正開發人員背景的人來說可能是一個簡單的解決方案?

date_range_begin <- "2021-07-11"
date_range_end <- "2021-07-17"

ggplot(data_frame, aes(x = spend, y = orders, color = factor)) +
  geom_point() + stat_smooth(method = 'lm', se = FALSE) +
  labs(title = "Title here", subtitle = "Orders ~ Spend") +
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) +
  theme(plot.subtitle = element_text(hjust = 0.5)) +
  labs(caption = "Week of date_range_begin date_range_end")

在此處輸入圖片說明

這是一種方法。 該字符串由sprintf形成,以將變量的值放在正確的位置。 另一種可能是?paste

library(ggplot2)

date_range_begin <- "2021-07-11"
date_range_end <- "2021-07-17"

ggplot(data_frame, aes(x = spend, y = orders, color = factor)) +
  geom_point() + 
  stat_smooth(method = 'lm', formula = y ~ x, se = FALSE) +
  labs(
    title = "Title here", 
    subtitle = "Orders ~ Spend",
    caption = sprintf("Week of %s to %s", date_range_begin, date_range_end)
  ) +
  theme(
    plot.title = element_text(hjust = 0.5, face = "bold"),
    plot.subtitle = element_text(hjust = 0.5)
  )

在此處輸入圖片說明


數據

data_frame <- iris [3:5]
names(data_frame) <- c("spend", "orders", "factor")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM