ggplot 图表中的混合格式轴标签
Mixed format axis labels in ggplot chart
是否可以使用带有日期格式标签和文本格式标签的 ggplot 轴?
以下代码生成一个图表,其中包含从 Jan-18 到 Jun-19 的日期格式的 x 轴。
#create plot with line and points.
#colour of points based on colour column of meltdf2
ggplot(data = meltdf2, aes(x = Month, y = variable, group = variable)) +
geom_line(linetype = "dashed", colour = "grey") +
geom_point(aes(colour = meltdf2$colour, size = 3)) +
geom_point(data = melt_change, aes(shape = value, size = 3)) +
scale_colour_manual(values = pal, limits = names(pal), guide_legend("Risk Level")) +
scale_shape_manual(values = sh_pal, limits = names(sh_pal), guide_legend("Latest Change")) +
scale_x_date(date_breaks = "1 month" , date_labels = "%b-%y") +
theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank(),
axis.line.x = element_line(colour = "darkgrey"), axis.title.x=element_blank(),
panel.background = element_rect(fill = "white"),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
legend.position = c(0.2,0.70),
legend.key.size = unit(1.5,"line")) +
guides(colour = guide_legend(override.aes = list(size = 3)), shape = guide_legend(override.aes = list(size = 5))) +
scale_size(guide = "none")
但是,我希望将 Jun-19 日期标签替换为显示 "Change" 的文本标签。有没有办法在不单独指定所有标签的情况下做到这一点?
我已尝试使用以下代码生成可用作 scale_x_date
函数中的 labels
参数的向量:
x_labels <- (as.Date(unique(meltdf2$Month)))
x_labels <- sort(x_labels)
x_labels <- c(x_labels,"Change")
但是矢量似乎没有正确排序日期,文本 "Change" 值被 NA 替换。
由于示例无法完全重现,我创建了一个包含虚构数据的响应。
请注意,我必须为所分析的时间段 ([=11=]) 创建一个新列,因此它是一个因素,我在其中添加了名称“Change".
后来,我根据感兴趣的日期 (Dates
),使用 forcats
、函数 fct_reorder
对级别重新排序。
library(dplyr)
library(lubridate)
library(ggplot2)
library(forcats)
set.seed(1)
Period_x <- data.frame(Period = seq(lubridate::mdy('jan-01-2018'),
lubridate::mdy('may-01-2019'),
by = 'month'))
Period_x <- Period_x %>%
dplyr::mutate(Period = format(Period, '%b-%y'))
Period_x <- rbind(Period_x, "Change")
Period2 <- seq(lubridate::mdy('jan-01-2018'),
lubridate::mdy('jun-01-2019'),
by = 'month')
Period_x <- Period_x %>%
dplyr::mutate(Dates = Period2,
y = rnorm(18),
Period = as.factor(Period),
Period = forcats::fct_reorder(Period,
Dates))
ggplot2::ggplot(Period_x) +
geom_point(aes(x = Period, y = y))
是否可以使用带有日期格式标签和文本格式标签的 ggplot 轴?
以下代码生成一个图表,其中包含从 Jan-18 到 Jun-19 的日期格式的 x 轴。
#create plot with line and points.
#colour of points based on colour column of meltdf2
ggplot(data = meltdf2, aes(x = Month, y = variable, group = variable)) +
geom_line(linetype = "dashed", colour = "grey") +
geom_point(aes(colour = meltdf2$colour, size = 3)) +
geom_point(data = melt_change, aes(shape = value, size = 3)) +
scale_colour_manual(values = pal, limits = names(pal), guide_legend("Risk Level")) +
scale_shape_manual(values = sh_pal, limits = names(sh_pal), guide_legend("Latest Change")) +
scale_x_date(date_breaks = "1 month" , date_labels = "%b-%y") +
theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank(),
axis.line.x = element_line(colour = "darkgrey"), axis.title.x=element_blank(),
panel.background = element_rect(fill = "white"),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
legend.position = c(0.2,0.70),
legend.key.size = unit(1.5,"line")) +
guides(colour = guide_legend(override.aes = list(size = 3)), shape = guide_legend(override.aes = list(size = 5))) +
scale_size(guide = "none")
但是,我希望将 Jun-19 日期标签替换为显示 "Change" 的文本标签。有没有办法在不单独指定所有标签的情况下做到这一点?
我已尝试使用以下代码生成可用作 scale_x_date
函数中的 labels
参数的向量:
x_labels <- (as.Date(unique(meltdf2$Month)))
x_labels <- sort(x_labels)
x_labels <- c(x_labels,"Change")
但是矢量似乎没有正确排序日期,文本 "Change" 值被 NA 替换。
由于示例无法完全重现,我创建了一个包含虚构数据的响应。
请注意,我必须为所分析的时间段 ([=11=]) 创建一个新列,因此它是一个因素,我在其中添加了名称“Change".
后来,我根据感兴趣的日期 (Dates
),使用 forcats
、函数 fct_reorder
对级别重新排序。
library(dplyr)
library(lubridate)
library(ggplot2)
library(forcats)
set.seed(1)
Period_x <- data.frame(Period = seq(lubridate::mdy('jan-01-2018'),
lubridate::mdy('may-01-2019'),
by = 'month'))
Period_x <- Period_x %>%
dplyr::mutate(Period = format(Period, '%b-%y'))
Period_x <- rbind(Period_x, "Change")
Period2 <- seq(lubridate::mdy('jan-01-2018'),
lubridate::mdy('jun-01-2019'),
by = 'month')
Period_x <- Period_x %>%
dplyr::mutate(Dates = Period2,
y = rnorm(18),
Period = as.factor(Period),
Period = forcats::fct_reorder(Period,
Dates))
ggplot2::ggplot(Period_x) +
geom_point(aes(x = Period, y = y))