R:在 ggplot 中将限制设置为 scale_x_yearqtr for yearqtr (zoo)
R: Setting limits to scale_x_yearqtr in ggplot for yearqtr (zoo)
我正在处理类似于以下摘录的数据集:
head(nomis.lng.agg)
quarter decile avg.val
1 2004 Q4 1 5.680000
2 2005 Q1 1 5.745763
3 2005 Q2 1 5.503341
4 2005 Q3 1 5.668224
5 2005 Q4 1 5.244604
6 2006 Q1 1 5.347222
变量 quarter 属于 class yearqtr
,由 zoo
生成。其余两列是数字。我目前正在生成一个使用以下 ggplot
语法的图:
ggplot(data = subset(x = df,
subset = df$decile== 1 |
df$decile== 10),
aes(x = quarter, y = avg.val, group = decile)) +
geom_line(aes(linetype=as.factor(decile)),
size = 1) +
scale_x_yearqtr(format = "%YQ%q", n = 5) +
xlab("Quarter") +
ylab("Average val") +
ggtitle("Plot") +
scale_linetype_discrete(name="Legend") +
theme(panel.background = element_blank(),
axis.line = element_line(colour = "black"),
axis.text = element_text(size = 12, colour = "black"),
axis.title = element_text(size = 14, colour = "black"),
panel.grid.minor = element_blank(),
panel.grid.major.y = element_line(colour = "gray"),
panel.grid.major.x = element_blank(),
axis.text = element_text(size = 12, colour = "black"),
legend.text = element_text(size = 12),
legend.title = element_text(size = 12),
legend.key.width=unit(1.5,"cm"),
legend.position = "bottom",
legend.key = element_rect(fill = "white"),
legend.background = element_rect(colour = "black"),
plot.title = element_text(face="bold"),
plot.background = element_rect(colour = "black"))
除 x 轴外,该图几乎完美。当前的 x 轴看起来像这样:
我的重点是代码scale_x_yearqtr(format = "%YQ%q", n = 5)
。由于我的数据从 2004 Q4 开始,我对绘制 2004 Q1 不感兴趣,但我想设置限制:
scale_x_yearqtr(format = "%YQ%q",
limits=c(min(quarter), max=max(quarter)))
然而,这并没有产生预期的结果,尽管:
min(df$quarter)
[1] "2004 Q4"
我认为您只是没有正确指定 limits
。此外,要更好地控制外观,请使用 breaks
参数(而不是 n
)。
# some data
df <- data.frame(x = as.yearqtr(2004 + seq(3, 8)/4), y = sample(1:6))
# setting limits only
ggplot(data = df, aes(x, y, group = 1)) +
geom_line() +
scale_x_yearqtr(limits = c(min(df$x), max(df$x)),
format = "%YQ%q")
# setting breaks
ggplot(data = df, aes(x, y, group = 1)) +
geom_line() +
scale_x_yearqtr(breaks = seq(from = min(df$x), to = max(df$x), by = 0.25),
format = "%YQ%q")
我正在处理类似于以下摘录的数据集:
head(nomis.lng.agg)
quarter decile avg.val
1 2004 Q4 1 5.680000
2 2005 Q1 1 5.745763
3 2005 Q2 1 5.503341
4 2005 Q3 1 5.668224
5 2005 Q4 1 5.244604
6 2006 Q1 1 5.347222
变量 quarter 属于 class yearqtr
,由 zoo
生成。其余两列是数字。我目前正在生成一个使用以下 ggplot
语法的图:
ggplot(data = subset(x = df,
subset = df$decile== 1 |
df$decile== 10),
aes(x = quarter, y = avg.val, group = decile)) +
geom_line(aes(linetype=as.factor(decile)),
size = 1) +
scale_x_yearqtr(format = "%YQ%q", n = 5) +
xlab("Quarter") +
ylab("Average val") +
ggtitle("Plot") +
scale_linetype_discrete(name="Legend") +
theme(panel.background = element_blank(),
axis.line = element_line(colour = "black"),
axis.text = element_text(size = 12, colour = "black"),
axis.title = element_text(size = 14, colour = "black"),
panel.grid.minor = element_blank(),
panel.grid.major.y = element_line(colour = "gray"),
panel.grid.major.x = element_blank(),
axis.text = element_text(size = 12, colour = "black"),
legend.text = element_text(size = 12),
legend.title = element_text(size = 12),
legend.key.width=unit(1.5,"cm"),
legend.position = "bottom",
legend.key = element_rect(fill = "white"),
legend.background = element_rect(colour = "black"),
plot.title = element_text(face="bold"),
plot.background = element_rect(colour = "black"))
除 x 轴外,该图几乎完美。当前的 x 轴看起来像这样:
我的重点是代码scale_x_yearqtr(format = "%YQ%q", n = 5)
。由于我的数据从 2004 Q4 开始,我对绘制 2004 Q1 不感兴趣,但我想设置限制:
scale_x_yearqtr(format = "%YQ%q",
limits=c(min(quarter), max=max(quarter)))
然而,这并没有产生预期的结果,尽管:
min(df$quarter)
[1] "2004 Q4"
我认为您只是没有正确指定 limits
。此外,要更好地控制外观,请使用 breaks
参数(而不是 n
)。
# some data
df <- data.frame(x = as.yearqtr(2004 + seq(3, 8)/4), y = sample(1:6))
# setting limits only
ggplot(data = df, aes(x, y, group = 1)) +
geom_line() +
scale_x_yearqtr(limits = c(min(df$x), max(df$x)),
format = "%YQ%q")
# setting breaks
ggplot(data = df, aes(x, y, group = 1)) +
geom_line() +
scale_x_yearqtr(breaks = seq(from = min(df$x), to = max(df$x), by = 0.25),
format = "%YQ%q")