r - ggplot2 - 在 x 轴上按季度绘制的线图

标签 r plot ggplot2 time-series rstudio

这个问题在这里已经有了答案:





Create line graph with ggplot2, using time periods as x-variable

(1 个回答)


7年前关闭。




我正在尝试使用 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 绘制 Assets ——由 concatenate 形成的 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.) 是我们如何显示我如何绘制 Assets 的季度值,但在 x 轴上只显示 2000、2001、2002,...这是因为因为我有 2001-1, 2001-2, ... , 2014-1. x 轴看起来很拥挤,所以我想看看我是否可以显示类似的东西

2000 2001 2003 然后在它们之间显示四分之一值。

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

最佳答案

这是你要找的吗?

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

enter image description here

关于r - ggplot2 - 在 x 轴上按季度绘制的线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26797193/

相关文章:

python - 将 netcdf 文件的所有时间步长绘制成 map

r - 如何调整geom_point()的scale_alpha?

python - "reset" Bokeh 图的命令是什么?

r - 将整体 bar 和 perc 标签添加到 geom_bar

r - ggplot2 aes_string() 无法处理以数字开头或包含空格的名称

r - 如何在 R 中的矢量图上绘制 igraph 对象

r - 如何将35GB数据加载到R中?

r - 如何使用ggplot绘制3D散点图?

r - 使用 ggplot2 进行多重绘图

r - 使用 ggplot 创建多个堆叠的 fiddle 图