【问题标题】:How to show each year-quarter in x-axis in the time plot generated by ggplot如何在 ggplot 生成的时间图中在 x 轴上显示每个年度季度
【发布时间】:2019-12-20 05:07:46
【问题描述】:

我想使用 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()

【问题讨论】:

标签: r ggplot2


【解决方案1】:

您可以使用例如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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多