将月份映射到用星期建立的轴?

时间:2014-12-02 14:16:06

标签: r ggplot2 lubridate

我有很多这样的情节:

ggplot(aes(x=yearday, y=numeric.result)) + ...

其中" yearday"是使用yday()中的lubridate函数获得的。

我现在希望x轴标签能够显示数月而不是周期,而我似乎无法找到一种直接的方式来实现这一目标。肯定涉及scale_x_continous,休息时间为#天/月,标签为abbr。几个月,但我似乎无法正确使用语法。

提前感谢!!

dput(mydat)

structure(list(ydays = c(354, 354, 20, 53, 80, 94, 138, 166, 
207, 236, 285, 332, 353, 353, 45, 64, 100, 136, 136, 176, 211, 
211, 240, 240, 269, 288, 288, 323, 347, 24, 49, 77, 100, 140, 
176, 211, 238, 267, 289, 324, 345, 22, 41, 64, 112, 140, 155, 
203, 218, 252, 280, 322, 343, 26, 42, 76, 119, 146, 168, 209, 
229, 259, 293, 334, 342, 19, 38, 66, 109, 138, 164, 192, 229, 
255, 278, 305, 339, 17, 37, 67, 108, 122, 170, 191, 228, 261, 
296, 318, 347, 17, 50, 78, 106, 134, 156, 204, 232, 247, 288, 
317, 345, 14, 14, 51, 51, 84, 107, 141, 168, 203, 231, 282, 316, 
345, 345, 345, 5, 51, 69, 69, 103, 133, 167, 195, 217, 258, 285, 
348, 19, 40, 75, 97, 145, 159, 194, 221, 251, 280, 312, 340, 
18, 39, 67, 108, 143, 165, 187, 227, 256, 283, 313, 339, 339, 
10, 44, 72, 101, 135, 156, 198, 226, 254, 276, 312, 338, 36, 
91, 133, 154, 218, 42, 64, 132, 162, 188, 252, 281, 307), result = c(0.22, 
1.688, 0.606, 0.794, 1.211, 1.013, 1.272, 1.142, 0.812, 0.557, 
0.421, 0.91, 1.171, 0.522, 1.002, 1.028, 0.917, 0.634, 1.494, 
0.972, 1.194, 0.609, 1.466, 0.359, 0.418, 0.278, 1.291, 0.754, 
0.798, 0.404, 0.495, 0.621, 0.859, 0.625, 0.851, 0.226, 0.147, 
0.264, 0.306, 0.189, 0.247, 0.646, 0.893, 0.519, 1.149, 0.876, 
0.236, 0.943, 0.846, 0.724, 0.417, 0.626, 0.667, 0.461, 0.794, 
0.558, 0.3, 0.61, 0.542, 0.35, 0.569, 0.172, 0.082, 0.118, 0.278, 
0.468, 0.403, 0.455, 0.397, 0.357, 0.357, 0.245, 0.208, 0.312, 
0.12, 0.168, 0.099, 0.383, 0.638, 0.457, 0.637, 0.723, 0.553, 
0.591, 0.246, 0.488, 0.169, 0.505, 0.248, 0.577, 0.436, 0.496, 
0.519, 0.47, 0.439, 0.162, 0.084, 0.06, 0.209, 0.104, 0.116, 
1.31, 0.306, 1.355, 0.122, 0.391, 0.467, 0.7, 0.538, 0.407, 0.39, 
0.124, 0.152, 1.185, 0.154, 1.26, 0.297, 0.778, 1.491, 0.378, 
0.27, 0.629, 0.574, 0.496, 0.432, 0.374, 0.116, 0.28, 0.409, 
0.624, 0.501, 0.973, 0.152, 0.472, 0.674, 0.2, 0.234, 0.2, 0.02, 
0.362, 0.3, 0.44, 1.22, 0.415, 0.782, 0.66, 0.567, 0.418, 0.333, 
0.26, 0.166, 0.414, 0.414, 0.134, 0.509, 0.43, 0.218, 0.437, 
0.304, 0.344, 0.258, 0.17, 0.175, 0.487, 0.506, 0.293, 0.435, 
0.443, 0.352, 0.499, 0.456, 0.577, 0.479, 0.241, 0.552, 0.262, 
0.199, 0.131)), .Names = c("ydays", "result"), row.names = c(NA, 
-178L), class = "data.frame")

CODE

ggplot(mydat,aes(x = ydays, y = result)) + geom_point() +  geom_line() + theme_bw() 

所以,在这里你可以看到数据在第1天(1月1日)到第365天(12月31日)正确地绘制,但我想简单地说明在x轴上指示的月份以便清楚地阅读图表。请注意,没有与x轴相关的年份。

希望这有助于解释我的情况! 最好!!

2 个答案:

答案 0 :(得分:3)

如何添加scale_x_continuous?可能有更好的方法,但以下工作:

scale_x_continuous(breaks = cumsum(c(1, 31, 30, 28, 30, 31, 30, 31, 31, 30, 31, 30)), 
                   label = c('Jan', 'Feb', 'Mar', 'Apr', 'Mai', 'June', 
                             'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'))

答案 1 :(得分:0)

这个怎么样(所有月份都有30天,例如简洁)?

data <- data.frame(yday=1:360, values=rnorm(360))

months <- c("Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec")

labels <- rep("", times=360)
labels[1 + (0:11 * 30)] <- months

ggplot(data, aes(x=yday, y=values)) +
    scale_x_continuous(breaks=data$yday, labels=labels) + 
    geom_point() +  geom_line() + theme_bw()