提问人:rajvijay 提问时间:11/7/2014 更新时间:11/7/2014 访问量:14301
ggplot2 - x 轴上按年季度绘制的线图 [复制]
ggplot2 - line plot by year-quarter on the x axis [duplicate]
问:
我正在尝试将 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?
我想知道是不是因为年份qtr不是数字?会有什么问题?
我的问题 2.)是我们如何显示我如何绘制资产的季度值,然后在 x 轴上只显示 2000、2001、2002、...这是因为因为我有 2001-1、2001-2、...、2014-1。x 轴看起来非常拥挤,所以我试图看看我是否可以显示类似的东西
2000、2001、2003,然后在它们之间显示季度值。
非常感谢。感谢任何建议。
答:
1赞
KFB
11/7/2014
#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
评论
0赞
KFB
11/7/2014
关于你的问题 2,不要认为只显示 2000 年是个好主意,...这没有信息。如果空间太忙,您可以随时调整标签的“角度”或“倾斜”。
0赞
rajvijay
11/11/2014
这成功了。我使用了 theme(axis.text.x=element_text(angle=-45, hjust=0.001)) 和scale_axis_discrete将断点指定为每年第一季度的组合。
评论
ggplot(data = df, aes(x=yearqtr, y=assets, group=1)) + geom_line()