如何设置此 `geom_tile` 的 y 轴,使其在每个点都有格式化的 `yearmon` 标签?
How can I set the y axis of this `geom_tile` to have a formatted `yearmon` label at each point?
我有一个热图显示了一个值,y 轴是观测值的年月配对,底部轴是观测值的小时数。数据保存在 data.table
对象中。
默认情况下 ggplot2
图形如下所示:
ggplot(repeatability, aes(x = iHrMi, y = iYrMo, fill = erraticity)) +
geom_tile() +
facet_grid(. ~ off) +
scale_x_discrete(name = "Time", breaks = c("00:00", "12:00"))
这很好,但我的目标是让 y 轴上的标签包含月份的缩写名称,而不是数字,并保留它们的顺序。 zoo
具有 ggplot2
的扩展,允许您绘制 yearmon
对象,如下所示:
ggplot(repeatability, aes(x = iHrMi, y = zoo::as.yearmon(iYrMo), fill = erraticity, group = iYrMo)) +
geom_tile() +
facet_grid(. ~ off) +
zoo::scale_y_yearmon(name = "Year Month", expand = c(0,0)) +
scale_x_discrete(name = "Time", breaks = c("00:00", "12:00"))
格式正确,但标签数量不正确,我每个月要一个。此外,如果 expand
保留为默认值,则 y 轴会扩展并包括那里没有数据的时间段。
如果我提供一个 n
参数,要获得 12 个标签,就会发生这种情况:
我没有得到十二行,但我得到了一个额外的奇数标签,超出了 x 轴截距。
ggplot(repeatability, aes(x = iHrMi, y = zoo::as.yearmon(iYrMo), fill = erraticity, group = iYrMo)) +
geom_tile() +
facet_grid(. ~ off) +
zoo::scale_y_yearmon(name = "Year Month", expand = c(0,0), n = 12) +
scale_x_discrete(name = "Time", breaks = c("00:00", "12:00"))
这是用来制作它的 table 的头部。恐怕我不确定如何以有用的方式输入完整的 table 。如果有人对如何包含更完整的版本有任何有用的建议,我会更新它:
structure(list(iYrMo = c("2013-08", "2013-08", "2013-08", "2013-08",
"2013-08", "2013-08"), iHrMi = c("00:00", "00:30", "01:00", "01:30",
"02:00", "02:30"), off = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Weekday",
"Weekend/Holiday"), class = "factor"), fit = c(0.883255368890743,
0.888802101750935, 0.887399903327103, 0.896846543832244, 0.895936947283074,
0.898059799540441), erraticity = structure(c(4L, 4L, 4L, 4L,
4L, 4L), .Label = c("Most", "More", "Less", "Least"), class = "factor")), .Names = c("iYrMo",
"iHrMi", "off", "fit", "erraticity"), class = c("data.table",
"data.frame"), row.names = c(NA, -6L))
注意 yearmon 只是一个 double 类型,0
等于 Jan 0000
,2013 + 6/12
等于 July 2013
您可以使用 limits 和 breaks 参数(与 ggplot 中的 scale_y_continuous
相同):
library(zoo)
ggplot(repeatability, aes(x = iHrMi, y = as.yearmon(iYrMo), fill = erraticity, group = iYrMo)) +
geom_tile() +
facet_grid(. ~ off) +
scale_y_yearmon(name = "Year Month", limits = c(2013,2015), breaks = seq(2013,2015, by = 1/12)) +
scale_x_discrete(name = "Time", breaks = c("00:00", "12:00"))
您的可重现数据给出:
乱七八糟的,不过轴是对的
我有一个热图显示了一个值,y 轴是观测值的年月配对,底部轴是观测值的小时数。数据保存在 data.table
对象中。
默认情况下 ggplot2
图形如下所示:
ggplot(repeatability, aes(x = iHrMi, y = iYrMo, fill = erraticity)) +
geom_tile() +
facet_grid(. ~ off) +
scale_x_discrete(name = "Time", breaks = c("00:00", "12:00"))
这很好,但我的目标是让 y 轴上的标签包含月份的缩写名称,而不是数字,并保留它们的顺序。 zoo
具有 ggplot2
的扩展,允许您绘制 yearmon
对象,如下所示:
ggplot(repeatability, aes(x = iHrMi, y = zoo::as.yearmon(iYrMo), fill = erraticity, group = iYrMo)) +
geom_tile() +
facet_grid(. ~ off) +
zoo::scale_y_yearmon(name = "Year Month", expand = c(0,0)) +
scale_x_discrete(name = "Time", breaks = c("00:00", "12:00"))
格式正确,但标签数量不正确,我每个月要一个。此外,如果 expand
保留为默认值,则 y 轴会扩展并包括那里没有数据的时间段。
如果我提供一个 n
参数,要获得 12 个标签,就会发生这种情况:
我没有得到十二行,但我得到了一个额外的奇数标签,超出了 x 轴截距。
ggplot(repeatability, aes(x = iHrMi, y = zoo::as.yearmon(iYrMo), fill = erraticity, group = iYrMo)) +
geom_tile() +
facet_grid(. ~ off) +
zoo::scale_y_yearmon(name = "Year Month", expand = c(0,0), n = 12) +
scale_x_discrete(name = "Time", breaks = c("00:00", "12:00"))
这是用来制作它的 table 的头部。恐怕我不确定如何以有用的方式输入完整的 table 。如果有人对如何包含更完整的版本有任何有用的建议,我会更新它:
structure(list(iYrMo = c("2013-08", "2013-08", "2013-08", "2013-08",
"2013-08", "2013-08"), iHrMi = c("00:00", "00:30", "01:00", "01:30",
"02:00", "02:30"), off = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Weekday",
"Weekend/Holiday"), class = "factor"), fit = c(0.883255368890743,
0.888802101750935, 0.887399903327103, 0.896846543832244, 0.895936947283074,
0.898059799540441), erraticity = structure(c(4L, 4L, 4L, 4L,
4L, 4L), .Label = c("Most", "More", "Less", "Least"), class = "factor")), .Names = c("iYrMo",
"iHrMi", "off", "fit", "erraticity"), class = c("data.table",
"data.frame"), row.names = c(NA, -6L))
注意 yearmon 只是一个 double 类型,0
等于 Jan 0000
,2013 + 6/12
等于 July 2013
您可以使用 limits 和 breaks 参数(与 ggplot 中的 scale_y_continuous
相同):
library(zoo)
ggplot(repeatability, aes(x = iHrMi, y = as.yearmon(iYrMo), fill = erraticity, group = iYrMo)) +
geom_tile() +
facet_grid(. ~ off) +
scale_y_yearmon(name = "Year Month", limits = c(2013,2015), breaks = seq(2013,2015, by = 1/12)) +
scale_x_discrete(name = "Time", breaks = c("00:00", "12:00"))
您的可重现数据给出:
乱七八糟的,不过轴是对的