我想在横轴上显示月份(缩写形式),并将相应的年份打印一次。我知道如何显示月份年份:
在此处输入图片说明
一年中不必要的重复会打乱标签。相反,我想要这样的东西:
在此处输入图片说明
除了年份将 在月份下方打印。
我在轴标签上方打印了年份,因为这是我能做的最好的事情。这是对annotate()功能的限制,如果该功能位于绘图区域之外,则会受到限制。我知道基于的可能解决方法annotate_custom(),但是我无法使它们与日期对象一起使用(我没有尝试将日期转换为数字并再次返回到日期,因为它看起来比希望的要复杂得多)
我想知道是否dup_axis()可以为此目的劫持新产品。如果可以将重复的轴发送到面板的另一侧,而不是将重复的轴发送到面板的另一侧,则可能只需要设置一个panel.grid.major消隐的轴并将标签设置为%b,另一个轴将panel.grid.minor消隐,标签设置为%Y。(另一个挑战是,年份标签将改为10月而不是1月)
这些问题是相关的。但是,据我所知,该annotate_custom()功能textGrob()与日期不能很好地配合使用。
我如何在ggplot2中的x轴下方添加注释
在ggplot2生成的情节下面显示文本
数据和基本代码如下:
library("ggplot2")
library("scales")
ggplot(data = df, aes(x = Date, y = value)) + geom_line() +
scale_x_date(date_breaks = "2 month", date_minor_breaks = "1 month", labels = date_format("%b %Y")) +
xlab(NULL)
ggplot(data = df, aes(x = Date, y = value)) + geom_line() +
scale_x_date(date_minor_breaks = "2 month", labels = date_format("%b")) +
annotate(geom = "text", x = as.Date("1719-10-01"), y = 0, label = "1719") +
annotate(geom = "text", x = as.Date("1720-10-01"), y = 0, label = "1720") +
xlab(NULL)
# data
df <- structure(list(Date = structure(c(-91455, -91454, -91453, -91452,
-91451, -91450, -91448, -91447, -91446, -91445, -91444, -91443,
-91441, -91440, -91439, -91438, -91437, -91436, -91434, -91433,
-91431, -91430, -91429, -91427, -91426, -91425, -91424, -91423,
-91422, -91420, -91419, -91418, -91417, -91416, -91415, -91413,
-91412, -91411, -91410, -91409, -91408, -91406, -91405, -91404,
-91403, -91402, -91401, -91399, -91398, -91397, -91396, -91395,
-91394, -91392, -91391, -91390, -91389, -91388, -91387, -91385,
-91384, -91382, -91381, -91380, -91379, -91377, -91376, -91375,
-91374, -91373, -91372, -91371, -91370, -91369, -91368, -91367,
-91366, -91364, -91363, -91362, -91361, -91360, -91359, -91357,
-91356, -91355, -91354, -91353, -91352, -91350, -91349, -91348,
-91347, -91346, -91345, -91343, -91342, -91341, -91340, -91339,