每 N 个月将网格线添加到以日期为 x 轴的 geom_point 图中
Add grid lines every N months to geom_point plot with date as x-axis
我有一个数据框,如下所示:
df <- data.frame(list(Name = c("A", "A", "A", "A", "A",
"B", "B", "B", "B", "B"),
Date_Types = c("Date1", "Date2",
"Date3","Date4", "Date5",
"Date1", "Date2", "Date3",
"Date4", "Date5"), Dates =
structure(c(18903, 18965, 19081,
19116, 19172, 19083, 19097,
19219, 19249, 19341), class =
"Date")))
我想创建一个绘图并每 3 个月(即每年每季度)添加一次垂直网格线。我有以下代码:
library(ggplot2)
my_date_format <- function()
{
function(x)
{
m <- format(x,"%b")
y <- format(x,"%Y")
ifelse(duplicated(y),m,paste(m,y))
}
}
ggplot(df, aes(x = Dates, y = Name)) +
theme_classic() + geom_point(aes(shape = Date_Types), size = 8) +
theme(axis.line.y = element_blank(), axis.ticks = element_blank(),
axis.title = element_blank(), axis.text = element_text(size=22),
legend.text = element_text(size=22), legend.title = element_blank()) +
scale_x_date(date_break = "1 month", labels = my_date_format()) +
geom_vline(aes(xintercept= as.Date("2022-05-01")),
linetype = "dashed", color = "black", size = 1)
这意味着每年一月、四月、七月和十月的垂直线。所需的输出如下所示:
一种方法是使用 geom_vline
和您想要网格线的日期矢量手动添加“网格线”。您可以手动创建矢量或使用 lubridate
.
动态计算开始和结束季度
library(ggplot2)
library(lubridate)
min_date <- lubridate::floor_date(min(df$Dates), unit = "quarter")
max_date <- lubridate::ceiling_date(max(df$Dates), unit = "quarter")
quarters <- seq.Date(min_date, max_date, by = "quarter")
ggplot(df, aes(x = Dates, y = Name)) +
theme_classic() +
geom_vline(xintercept = quarters, color = "grey92", size = .5) +
geom_point(aes(shape = Date_Types), size = 8) +
theme(
axis.line.y = element_blank(), axis.ticks = element_blank(),
axis.title = element_blank(), axis.text = element_text(size = 12),
legend.text = element_text(size = 12), legend.title = element_blank()
) +
scale_x_date(date_break = "1 month", labels = my_date_format()) +
geom_vline(aes(xintercept = as.Date("2022-05-01")),
linetype = "dashed", color = "black", size = 1
)
您可以使用日期(从您的原始数据框生成)创建一个新的 data.frame
并将其提供给 geom_vline
,如下所示:
quarters <- data.frame(
q = seq.Date(as.Date(ISOdate(min(year(df$Dates)), 1, 1)),
max(df$Dates),
"3 months"))
ggplot(df, aes(x = Dates, y = Name)) +
theme_classic() + geom_point(aes(shape = Date_Types)) +
theme(axis.line.y = element_blank(), axis.ticks = element_blank(),
axis.title = element_blank(), legend.title = element_blank()) +
scale_x_date(date_break = "1 month", labels = my_date_format()) +
geom_vline(aes(xintercept = q), data = quarters,
linetype = "dashed", color = "black", size = 1)
使用 lubridate
包,创建一个季度序列并将其作为垂直线添加到图中:
library(lubridate)
... +
geom_vline(xintercept = seq(floor_date(min(df$Dates), unit = "quarter"),
ceiling_date(max(df$Dates), unit = "quarter"),
by = "1 quarter"))
完整代码:
df <- data.frame(list(Name = c("A", "A", "A", "A", "A",
"B", "B", "B", "B", "B"),
Date_Types = c("Date1", "Date2",
"Date3","Date4", "Date5",
"Date1", "Date2", "Date3",
"Date4", "Date5"), Dates =
structure(c(18903, 18965, 19081,
19116, 19172, 19083, 19097,
19219, 19249, 19341), class =
"Date")))
library(ggplot2)
my_date_format <- function()
{
function(x)
{
m <- format(x,"%b")
y <- format(x,"%Y")
ifelse(duplicated(y),m,paste(m,y))
}
}
library(lubridate)
ggplot(df, aes(x = Dates, y = Name)) +
geom_vline(xintercept = seq(floor_date(min(df$Dates), unit = "quarter"),
ceiling_date(max(df$Dates), unit = "quarter"),
by = "1 quarter"),
color = "grey80") +
theme_classic() + geom_point(aes(shape = Date_Types), size = 8) +
theme(axis.line.y = element_blank(), axis.ticks = element_blank(),
axis.title = element_blank(), axis.text = element_text(size=12),
legend.text = element_text(size=22), legend.title = element_blank()) +
scale_x_date(date_break = "1 month", labels = my_date_format()) +
geom_vline(aes(xintercept= as.Date("2022-05-01")),
linetype = "dashed", color = "black", size = 1)
我有一个数据框,如下所示:
df <- data.frame(list(Name = c("A", "A", "A", "A", "A",
"B", "B", "B", "B", "B"),
Date_Types = c("Date1", "Date2",
"Date3","Date4", "Date5",
"Date1", "Date2", "Date3",
"Date4", "Date5"), Dates =
structure(c(18903, 18965, 19081,
19116, 19172, 19083, 19097,
19219, 19249, 19341), class =
"Date")))
我想创建一个绘图并每 3 个月(即每年每季度)添加一次垂直网格线。我有以下代码:
library(ggplot2)
my_date_format <- function()
{
function(x)
{
m <- format(x,"%b")
y <- format(x,"%Y")
ifelse(duplicated(y),m,paste(m,y))
}
}
ggplot(df, aes(x = Dates, y = Name)) +
theme_classic() + geom_point(aes(shape = Date_Types), size = 8) +
theme(axis.line.y = element_blank(), axis.ticks = element_blank(),
axis.title = element_blank(), axis.text = element_text(size=22),
legend.text = element_text(size=22), legend.title = element_blank()) +
scale_x_date(date_break = "1 month", labels = my_date_format()) +
geom_vline(aes(xintercept= as.Date("2022-05-01")),
linetype = "dashed", color = "black", size = 1)
这意味着每年一月、四月、七月和十月的垂直线。所需的输出如下所示:
一种方法是使用 geom_vline
和您想要网格线的日期矢量手动添加“网格线”。您可以手动创建矢量或使用 lubridate
.
library(ggplot2)
library(lubridate)
min_date <- lubridate::floor_date(min(df$Dates), unit = "quarter")
max_date <- lubridate::ceiling_date(max(df$Dates), unit = "quarter")
quarters <- seq.Date(min_date, max_date, by = "quarter")
ggplot(df, aes(x = Dates, y = Name)) +
theme_classic() +
geom_vline(xintercept = quarters, color = "grey92", size = .5) +
geom_point(aes(shape = Date_Types), size = 8) +
theme(
axis.line.y = element_blank(), axis.ticks = element_blank(),
axis.title = element_blank(), axis.text = element_text(size = 12),
legend.text = element_text(size = 12), legend.title = element_blank()
) +
scale_x_date(date_break = "1 month", labels = my_date_format()) +
geom_vline(aes(xintercept = as.Date("2022-05-01")),
linetype = "dashed", color = "black", size = 1
)
您可以使用日期(从您的原始数据框生成)创建一个新的 data.frame
并将其提供给 geom_vline
,如下所示:
quarters <- data.frame(
q = seq.Date(as.Date(ISOdate(min(year(df$Dates)), 1, 1)),
max(df$Dates),
"3 months"))
ggplot(df, aes(x = Dates, y = Name)) +
theme_classic() + geom_point(aes(shape = Date_Types)) +
theme(axis.line.y = element_blank(), axis.ticks = element_blank(),
axis.title = element_blank(), legend.title = element_blank()) +
scale_x_date(date_break = "1 month", labels = my_date_format()) +
geom_vline(aes(xintercept = q), data = quarters,
linetype = "dashed", color = "black", size = 1)
使用 lubridate
包,创建一个季度序列并将其作为垂直线添加到图中:
library(lubridate)
... +
geom_vline(xintercept = seq(floor_date(min(df$Dates), unit = "quarter"),
ceiling_date(max(df$Dates), unit = "quarter"),
by = "1 quarter"))
完整代码:
df <- data.frame(list(Name = c("A", "A", "A", "A", "A",
"B", "B", "B", "B", "B"),
Date_Types = c("Date1", "Date2",
"Date3","Date4", "Date5",
"Date1", "Date2", "Date3",
"Date4", "Date5"), Dates =
structure(c(18903, 18965, 19081,
19116, 19172, 19083, 19097,
19219, 19249, 19341), class =
"Date")))
library(ggplot2)
my_date_format <- function()
{
function(x)
{
m <- format(x,"%b")
y <- format(x,"%Y")
ifelse(duplicated(y),m,paste(m,y))
}
}
library(lubridate)
ggplot(df, aes(x = Dates, y = Name)) +
geom_vline(xintercept = seq(floor_date(min(df$Dates), unit = "quarter"),
ceiling_date(max(df$Dates), unit = "quarter"),
by = "1 quarter"),
color = "grey80") +
theme_classic() + geom_point(aes(shape = Date_Types), size = 8) +
theme(axis.line.y = element_blank(), axis.ticks = element_blank(),
axis.title = element_blank(), axis.text = element_text(size=12),
legend.text = element_text(size=22), legend.title = element_blank()) +
scale_x_date(date_break = "1 month", labels = my_date_format()) +
geom_vline(aes(xintercept= as.Date("2022-05-01")),
linetype = "dashed", color = "black", size = 1)