我如何简化X轴等级(年度季度R)

发布于 2025-01-25 21:42:53 字数 679 浏览 2 评论 0 原文

第三年的大学生在这里,我有一个经济学项目可以在15日回去。我们必须与RMD合作,但我并不真正熟悉该工具...因此,任何帮助都将不胜感激! 我的问题是我想在多年来在比利时的GGPLOT上展示GDP的演变。这是我的数据的样子(您可以看到时间列与四分之一

ggplot(data = PIB, aes(x=PIB$Time, y=PIB$Belgium, group=1))  + geom_line() + labs(x="Time", y="Belgian PIB")

[

! ] [2]] [2]

我对图形的外观非常满意,除了X轴外观。 想显示在线4/5时间戳,因此读者可以理解演变从2001年到2021年,并且GDP每季度都会更改

该列的格式确实使我失望了。我 这可能是一个愚蠢的问题,但欢迎任何帮助! [1]: https://i.sstatic.net/xvynw.png [2]: https://i.sstatic.net/1f1ll.1f1ll.png

3rd year uni student here, I have an econometry project to hand back on the 15th; we have to work with Rmd but I'm not really familiar with the tool... so any help would be much appreciated!
My problem here is that I want to display on a ggplot the evolution of GDP in Belgium throughout the years. Here's what my data looks like (you can see the Time column is organized with quarters [![Data to work with][1]][1]. I coded this:

ggplot(data = PIB, aes(x=PIB$Time, y=PIB$Belgium, group=1))  + geom_line() + labs(x="Time", y="Belgian PIB")

and here's the ouput I have:

[![ggplot evolution graph][2]][2]

I'm very satisfied with how the graph looks except for the X-axis. I looked online on many platforms but I can't for the life of me figure out how to organize the Time axis. The format of the column really throws me off.. I'd like for online 4/5 timestamps to be shown, so the reader can understand that the evolution goes from 2001 to 2021 and that the GDP changes every quarter.

I'm aware this is probably a stupid question but any help would be welcome!
[1]: https://i.sstatic.net/XvYNW.png
[2]: https://i.sstatic.net/1f1Ll.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琉璃梦幻 2025-02-01 21:42:53

您必须将时间列从字符向量转换为时间。

library(zoo)
PIB$Time = as.yearqtr(PIB$Time,format="%YQ%q")
PIB$Time = as.Date(PIB$Time)

然后尝试GGPLOT。

You have to convert the time column from a character vector to time.

library(zoo)
PIB$Time = as.yearqtr(PIB$Time,format="%YQ%q")
PIB$Time = as.Date(PIB$Time)

Then try ggplot.

蓝色星空 2025-02-01 21:42:53

将时间转换为YearQtr类,并与 scale_x_yearqtr 一起使用。请参阅?scale_x_yearqtr 如果默认不是您想要的,请查看其各种参数。

请注意 r 标记页面的信息特别是r问题应该提供文本输入,以便其他人可以将其复制并粘贴到他们的会话中。如果不将其全部重新播放,则不能轻易使用图像。在没有的情况下,我使用了末尾注释中显示的可重复输入。使用以下任何一个:

library(ggplot2)
library(zoo)

# 1
qplot(as.yearqtr(Time), Belgium, data = PIB, geom = "line", xlab = "") +
  scale_x_yearqtr()

# 2 - will default to scale_x_yearqtr even if not specified
qplot(as.yearqtr(Time), Belgium, data = PIB, geom = "line", xlab = "")

# 3 - convert to zoo series and plot that
Belgium <- read.zoo(PIB, FUN = as.yearqtr)
autoplot(Belgium) + xlab("")

“屏幕截图”

注释

PIB <-
structure(list(Time = c("2001Q1", "2001Q2", "2001Q3", "2001Q4", 
"2002Q1", "2002Q2", "2002Q3", "2002Q4"), Belgium = 1:8), 
class = "data.frame", row.names = c(NA, -8L))

Convert Time to yearqtr class and use with scale_x_yearqtr. See ?scale_x_yearqtr to review its various arguments if the default is not what you want.

Note the information at the top of the tag page and in particular R questions to SO should provide the input as text so that others can copy and paste it into their session. Images can't easily be used without retyping it all. In the absence of that I have used the reproducible input shown in the Note at the end. Use any of these:

library(ggplot2)
library(zoo)

# 1
qplot(as.yearqtr(Time), Belgium, data = PIB, geom = "line", xlab = "") +
  scale_x_yearqtr()

# 2 - will default to scale_x_yearqtr even if not specified
qplot(as.yearqtr(Time), Belgium, data = PIB, geom = "line", xlab = "")

# 3 - convert to zoo series and plot that
Belgium <- read.zoo(PIB, FUN = as.yearqtr)
autoplot(Belgium) + xlab("")

screenshot

Note

PIB <-
structure(list(Time = c("2001Q1", "2001Q2", "2001Q3", "2001Q4", 
"2002Q1", "2002Q2", "2002Q3", "2002Q4"), Belgium = 1:8), 
class = "data.frame", row.names = c(NA, -8L))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文