我有类似这样的数据,介于2018-03-25至2018-12-23之间的几周:
week child n freq
1 2018-03-25 FALSE 758 0.812
2 2018-03-25 TRUE 176 0.188
3 2018-04-01 FALSE 534 0.845
4 2018-04-01 TRUE 98 0.155
5 2018-04-08 FALSE 336 0.808
6 2018-04-08 TRUE 80 0.192
7 2018-04-15 FALSE 279 0.793
8 2018-04-15 TRUE 73 0.207
9 2018-04-22 FALSE 299 0.819
10 2018-04-22 TRUE 66 0.181
当我使用ggplot创建时间序列时,即使我在一月份没有数据,x轴上的日期标签也始终显示一月。如何删除此标签或调整日期标签以正确显示结束月份?
生成图的代码:
library(ggplot)
library(hrbrthemes)
ggplot(data, aes(week, freq, fill = child)) +
geom_area(position = "stack") +
scale_fill_manual(values = c("#006FB7", "#F18E00"),
labels = c("Adults", "Children")) +
scale_y_percent() +
theme_ipsum_rc(base_family = "Times New Roman", base_size = 10, grid = "none")
数据输入
structure(list(week = structure(c(17706, 17678, 17699, 17671, 17804, 17664, 17790, 17734, 17685, 17622, 17650, 17657, 17797,
17839, 17832, 17720, 17881, 17643, 17748, 17811, 17727, 17615, 17713, 17818, 17629, 17769,
17636, 17846, 17762, 17825, 17741, 17755, 17853, 17776, 17783, 17867, 17888, 17874, 17860,
17692, 17692, 17860, 17874, 17888, 17867, 17783, 17776, 17853, 17755, 17741, 17825, 17762,
17846, 17636, 17769, 17629, 17818, 17713, 17615, 17727, 17811, 17748, 17643, 17881, 17720,
17832, 17839, 17797, 17657, 17650, 17622, 17685, 17734, 17790, 17664, 17804, 17671, 17699,
17678, 17706), class = "Date"),
child = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE),
n = c(110L, 86L, 97L, 54L, 161L, 1408L, 206L, 131L, 205L, 534L, 241L, 300L, 179L, 110L, 74L, 108L, 84L, 299L,
112L, 204L, 78L, 758L, 158L, 187L, 336L, 50L, 279L, 44L, 78L, 212L, 69L, 92L, 55L, 97L, 110L, 73L, 15L,
101L, 59L, 1L, 1L, 32L, 42L, 6L, 25L, 37L, 32L, 18L, 27L, 20L, 61L, 22L, 12L, 73L, 12L, 80L, 44L, 37L,
176L, 18L, 46L, 25L, 66L, 18L, 22L, 15L, 22L, 35L, 58L, 45L, 98L, 37L, 23L, 36L, 242L, 27L, 9L, 16L,
12L, 14L),
freq = c(0.887, 0.878, 0.858, 0.857, 0.856, 0.853, 0.851, 0.851, 0.847, 0.845, 0.843, 0.838, 0.836, 0.833,
0.831, 0.831, 0.824, 0.819, 0.818, 0.816, 0.812, 0.812, 0.81, 0.81, 0.808, 0.806, 0.793, 0.786,
0.78, 0.777, 0.775, 0.773, 0.753, 0.752, 0.748, 0.745, 0.714, 0.706, 0.648, 0.5, 0.5, 0.352, 0.294,
0.286, 0.255, 0.252, 0.248, 0.247, 0.227, 0.225, 0.223, 0.22, 0.214, 0.207, 0.194, 0.192, 0.19,
0.19, 0.188, 0.188, 0.184, 0.182, 0.181, 0.176, 0.169, 0.169, 0.167, 0.164, 0.162, 0.157, 0.155,
0.153, 0.149, 0.149, 0.147, 0.144, 0.143, 0.142, 0.122, 0.113)),
class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -80L))
答案 0 :(得分:2)
如上所述,@ kath可以使用scale_x_date()
的功能:
library(tidyverse)
dat %>%
ggplot(aes(week, freq, fill = child)) +
geom_area(position = "stack") +
scale_fill_manual(values = c("#006FB7", "#F18E00"),
labels = c("Adults", "Children")) +
scale_x_date(
"Date",
breaks = seq(min(dat$week), max(dat$week), by = "month"),
date_labels = "%b, %Y",
) +
scale_y_continuous("Frquency", labels = scales::percent) +
ggthemes::theme_tufte()