我想使用ggplot生成每个年一季度的时间图,但是x轴上仅显示年份。 Q1,Q2,Q3,Q4全部丢失?如何解决这个问题呢。
> head(X02_4m_bb)
# A tibble: 6 x 7
Year Quarter Revenue CGS Gross_Profit_M Disp_Income_B YQ
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <yearqtr>
1 1995 1 1275. 1080. 195. 16351. 1995 Q1
2 1995 2 1438. 1228. 210. 16481. 1995 Q2
3 1995 3 1929. 1672. 257. 16695. 1995 Q3
4 1995 4 2576. 2246. 329. 16865. 1995 Q4
5 1996 1 1637. 1387. 250. 17122. 1996 Q1
6 1996 2 1779. 1510. 268. 17400. 1996 Q2
ggplot(X02_4m_bb, aes(x=YQ, y=Revenue, group=1)) +
geom_line()
答案 0 :(得分:0)
您可以使用例如scale_x_date_yq
来调整比例。
这是一个工作示例:
require(zoo)
set.seed(314)
dates <- expand.grid(year = 1990:1995, q = 1:4) %>%
mutate(YQ = as.yearqtr(paste0(year,' Q',q)))
data.frame(YQ = dates$YQ,
value = rnorm(nrow(dates))) %>%
ggplot(aes(x = YQ, y = value)) +
geom_line() +
scale_x_yearqtr(breaks = dates$YQ) +
theme(axis.text.x = element_text(angle = 45,hjust = 1, vjust = 1))