这个问题在这里已经有了答案:
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
关于r - ggplot2 - 在 x 轴上按季度绘制的线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26797193/