绘制季度数据并使用rstudio中的GGPLOT指定季度

发布于 2025-02-06 21:48:44 字数 1465 浏览 2 评论 0原文

我想绘制季度数据,其中将信息保存在变量yq中为:yyyy Q(e..g 2007 Q1),我是通过结合年份和季度(1,2,2,3,4)创建的:

df$yq = as.yearqtr(paste(df$Year, df$Quarter), "%Y %q")

环顾四周在线示例,我编写了此代码,但问题是它不能让我指定要在X轴上显示的几年 - 或者我似乎找不到一种方法来应用此类数据的休息时间。我有15年的数据,只想每3年显示一次标签,因此唯一的标签是2005.Q1,2008.Q1,2011.Q1,2014.Q1,2017.Q1。

感谢提示!

plot = ggplot(df, aes(x = yq, y = rate, color = "red")) +
  geom_point() +
  geom_line(aes(y = lin, color = "green"), size = 1) +
  geom_line(aes(y = quad, color = "brown"), size = 1) +
  geom_line(aes(y = cube, color = "blue"), size = 1) +
  geom_line(aes(y = log, color = "pink"), size = 1) +
  geom_line(aes(y = power_corr, color = "black"), size = 1) +
  geom_line(aes(y = exp_corr, color = "yellow"), size = 1) +
  scale_color_identity(name = "Models",
                       breaks = c("red", "green", "brown", "blue", "pink", "black", "yellow"),
                       labels = c("Observed", "Linear", "Quadratic", "Cubic", "Log", "Power", "Exponential"),
                       guide = "legend")

plot + 
theme(panel.grid.major = element_blank(), 
      panel.grid.minor = element_blank(), 
      panel.background = element_blank(), 
      axis.line = element_line(colour = "black"))+
  theme(axis.text.x = element_text(size = 8, angle=-45, hjust = 0.001), 
        legend.key = element_rect(color = NA, fill = NA))+
  labs(title="", x = "Year/quarters", y = "Smoking prevalence")+
  scale_x_yearqtr(format='%Y.Q%q')

I want to plot quarterly data, where the information is saved in the variable yq as: YYYY Q (e..g 2007 Q1), I created by combining Year and Quarter (1, 2, 3, 4):

df$yq = as.yearqtr(paste(df$Year, df$Quarter), "%Y %q")

Looking around on the examples online, I wrote this code but the problem is that it does not let me specify the years I want to display on the x-axis - or rather I can't seem to find a way to apply the breaks for this type of data. I have 15 years of data and would like to only display the label every 3 years, so that the only labels are 2005.Q1, 2008.Q1, 2011.Q1, 2014.Q1, 2017.Q1.

Grateful for tips!

plot = ggplot(df, aes(x = yq, y = rate, color = "red")) +
  geom_point() +
  geom_line(aes(y = lin, color = "green"), size = 1) +
  geom_line(aes(y = quad, color = "brown"), size = 1) +
  geom_line(aes(y = cube, color = "blue"), size = 1) +
  geom_line(aes(y = log, color = "pink"), size = 1) +
  geom_line(aes(y = power_corr, color = "black"), size = 1) +
  geom_line(aes(y = exp_corr, color = "yellow"), size = 1) +
  scale_color_identity(name = "Models",
                       breaks = c("red", "green", "brown", "blue", "pink", "black", "yellow"),
                       labels = c("Observed", "Linear", "Quadratic", "Cubic", "Log", "Power", "Exponential"),
                       guide = "legend")

plot + 
theme(panel.grid.major = element_blank(), 
      panel.grid.minor = element_blank(), 
      panel.background = element_blank(), 
      axis.line = element_line(colour = "black"))+
  theme(axis.text.x = element_text(size = 8, angle=-45, hjust = 0.001), 
        legend.key = element_rect(color = NA, fill = NA))+
  labs(title="", x = "Year/quarters", y = "Smoking prevalence")+
  scale_x_yearqtr(format='%Y.Q%q')

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

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

发布评论

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

评论(1

街角卖回忆 2025-02-13 21:48:44

没有提供数据,因此我们将使用一个简单的X和Y向量示例,并使用指示的断点和格式参数使用Scale_x_yearqtr。

library(ggplot2)
library(zoo)

# inputs
x <- as.yearqtr(2000) + 0:29
y <- 1:30

qplot(x, y) + 
  scale_x_yearqtr(breaks = seq(min(x), max(x), by = 3), format = "%Y.Q%q")

The data was not provided so we will use a simple example of x and y vectors and use scale_x_yearqtr with the indicated breaks and format arguments.

library(ggplot2)
library(zoo)

# inputs
x <- as.yearqtr(2000) + 0:29
y <- 1:30

qplot(x, y) + 
  scale_x_yearqtr(breaks = seq(min(x), max(x), by = 3), format = "%Y.Q%q")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文