【问题标题】:ggplot2 - line plot by year-quarter on the x axis [duplicate]ggplot2 - x 轴上按年季度绘制的线图 [重复]
【发布时间】:2015-01-04 00:08:12
【问题描述】:

我正在尝试使用 ggplot2 进行以下操作。我正在寻找折线图,但遇到了问题。我有两个相关的问题。

1.)

 df = read.table(text = "bank  filer    id  quarter year    loan    assets
 year qtr yearqtr code assets
 2001 1   2001-1    51       39007.16
 2001 2   2001-2    51       83337.32
 2001 3   2001-3    51      133618.83
 2001 4   2001-4    51      211263.55
 2002 1   2002-1    51       68034.41
 2002 3   2002-2    51      134005.24
 2002 3   2002-3    51      203544.39
 2002 4   2002-4    51      274482.43
 2003 1   2003-1    51       63188.83"
, sep ="", header = TRUE)

首先我希望按 yearqtr 绘制资产——yearqtr 由连接形成

  year and qtr.

要按季度绘制,我有以下代码:

 myplot <- ggplot(data = df, aes(x=yearqtr, y=assets))  + geom_line()

 myplot <- myplot + theme(panel.grid.major = element_blank(), panel.grid.minor =         element_blank(),  panel.background = element_blank(), axis.line = element_line(colour = "black"))

但我明白了

 geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

不知道是不是因为yearqtr不是数字?会是什么问题?

我的问题 2.) 是我们如何显示我如何绘制资产的季度值,但是在 x 轴上只显示 2000、2001、2002 ......这是因为因为我有 2001 -1,2001 年 2 月,...,2014 年 1 月。 x 轴看起来很拥挤,所以我想看看是否可以显示类似

的内容

2000 2001 2003 然后在它们之间显示季度值。

非常感谢。感谢任何建议。

【问题讨论】:

  • 当你用谷歌搜索/搜索错误消息时发生了什么?
  • 试试:ggplot(data = df, aes(x=yearqtr, y=assets, group=1)) + geom_line()

标签: r plot ggplot2 time-series rstudio


【解决方案1】:

这是你要找的吗?

myplot <- ggplot(data = df, aes(x=yearqtr, y=assets, group=1)) +
  geom_line()
myplot <- myplot + 
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.background = element_blank(), 
        axis.line = element_line(colour = "black")) +
  scale_y_continuous(labels = comma) +
  theme(axis.text.x=element_text(angle=-45, hjust=0.001))
myplot

【讨论】:

  • 关于你的问题 2,不要认为只显示 2000 是一个好主意,......这不是信息。如果空间太忙,您可以随时调整标签的“角度”或“倾斜度”。
  • 成功了。我使用了 theme(axis.text.x=element_text(angle=-45, hjust=0.001)) 和 scale_axis_discrete 的组合,将断点指定为每年的第一季度。
猜你喜欢
  • 2015-10-14
  • 1970-01-01
  • 2012-03-07
  • 2019-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多