ggplot折线图的问题

Posted

技术标签:

【中文标题】ggplot折线图的问题【英文标题】:Issues with ggplot line charts 【发布时间】:2022-01-12 22:47:28 【问题描述】:

我不知道为什么我在这个 ggplot 的灰色条中出现这些断断续续的线条,以及为什么每个图表的图例顺序不同。你能帮我解决这个问题吗?这是我的代码和数据的子样本。用于 ggplot 的变量中没有使用缺失值。

Forecast_plot_vanilla <- ggplot(Forecast_df, aes(date)) + 
  geom_tile(aes(alpha = Recession, y = 1), 
            fill = "grey", height = Inf) +
  scale_alpha_continuous(range = c(0, 1), breaks = c(0, 1), labels = c("0" = "Expansion", "1" 
= "Recession"))+
  geom_line(aes(y = ERVOLY, col="Actual Returns"), size = .8)+
  ylab('Compounded Returns Next 12 Months (%)')+
  labs(x = NULL, alpha = "Economic Cylcle", color = "Model Forecast",
       title = "Rising Inflation Expectations in 2021",
   subtitle = '"Actual Returns vs. AR(2) Model"')+
  theme(plot.background = element_blank(),
        panel.grid.minor = element_blank(),
        panel.grid.major= element_blank())+
  scale_color_manual(values=c(
    "Actual Returns" = "black"))

Forecast_plot_vanilla 

这是数据的一个子集

dput(head(Forecast_df, 10))

structure(list(INFEX = c(2.9961298, 3.0273917, 2.9937871, 3.1838587, 
3.3951941, 3.3770226, 3.4259232, 3.5322081, 3.3882517, 3.4201416
), ERVOLY = c(-8.77748883028093, -14.8416404279317, -9.02974052805671, 
-16.8303572233488, -18.7945822035285, -14.8777818020646, -14.519413479622, 
-15.6444594874336, -16.4584536211307, -19.3396158494671), date = structure(c(10926, 
10957, 10988, 11017, 11048, 11078, 11109, 11139, 11170, 11201
), class = "Date"), Expansion = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 
1), Recession = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), ERVOLY_lag1 = c(-1.73631437680128, 
-8.77748883028093, -14.8416404279317, -9.02974052805671, -16.8303572233488, 
-18.7945822035285, -14.8777818020646, -14.519413479622, -15.6444594874336, 
-16.4584536211307), ERVOLY_lag2 = c(0.826035972924055, -1.73631437680128, 
-8.77748883028093, -14.8416404279317, -9.02974052805671, -16.8303572233488, 
-18.7945822035285, -14.8777818020646, -14.519413479622, -15.6444594874336
), ERVOL_vanilla = c(-0.86668297866284, -7.49039800107863, -12.8520494772519, 
-6.48762453981438, -14.3713769094631, -15.6549081560956, -11.6216205302654, 
-11.4929227320897, -12.7635844039787, -13.4320398169549), ERVOL_twofactor = 
c(-0.827189244172975, 
-7.46356815233837, -12.8829528152664, -6.60151291973358, -14.4185578850936, 
-15.7770171682792, -11.7743725647707, -11.6110549705504, -12.8692222977468, 
-13.5488959720254), ERVOL_threefactor = c(-0.923203527477895, 
-7.4933399582189, -12.8706459303638, -6.71159009749288, -14.4375623857157, 
-15.8077186223044, -11.8668964436481, -11.7032745502137, -12.9273687852697, 
-13.6070975934917), ERVOL_AR2 = c(-1.83716914438012, -8.676457359149, 
-14.2220911732806, -7.99373494394322, -16.1873977312507, -17.5901523147866, 
-13.5440099520562, -13.4658503981688, -14.6133744990458, -15.3449421615658
)), row.names = 140:149, class = "data.frame")

【问题讨论】:

是geom_tile()引起的。如果您不想要垂直的灰线,请删除该线。 我确实想要灰色条,但我不希望它没有像原来那样填充。 【参考方案1】:

问题在于geom_tile 为每个图块选择相同的宽度。但是,由于月份的天数不同,宽度基于 2 月的天数,即宽度为 28 天。对于所有其他月份,您最终会得到一些最多 3 天的空白区域,这是您的灰线。

摆脱灰线的一个选择是切换到geom_rect,我使用lubridate::ceiling_date 来获取每个“图块”的结束日期:

library(ggplot2)

Forecast_df$Recession[3:5] <- 1

ggplot(Forecast_df, aes(date)) + 
  geom_rect(aes(xmin = date, xmax = lubridate::ceiling_date(date, "month"), 
                ymin = -Inf, ymax = Inf, alpha = Recession), 
            fill = "grey") +
  geom_line(aes(y = ERVOLY, col="Actual Returns"), size = .8) +
  scale_alpha_continuous(range = c(0, 1), breaks = c(0, 1), labels = c("0" = "Expansion", "1" = "Recession"))+
  ylab('Compounded Returns Next 12 Months (%)')+
  labs(x = NULL, alpha = "Economic Cylcle", color = "Model Forecast",
       title = "Rising Inflation Expectations in 2021",
       subtitle = '"Actual Returns vs. AR(2) Model"')+
  theme(plot.background = element_blank(),
        panel.grid.minor = element_blank(),
        panel.grid.major= element_blank())+
  scale_color_manual(values=c("Actual Returns" = "black"))

【讨论】:

非常感谢斯特凡。如何解决图例问题? 您可以通过scale_xxx_yyy(..., guide = guide_legend(order = 1)) 设置图例的顺序,以使图例出现在顶部,... = 2 用于第二个和儿子。默认情况下,ggplot2 将通过“......秘密算法”(?guide_legend)(: 你是男人斯特凡!

以上是关于ggplot折线图的问题的主要内容,如果未能解决你的问题,请参考以下文章

ggplot折线图的问题

多个组的ggplot条形图+折线图

R语言ggplot2可视化:使用geom_line函数可视化折线图并自定义设置折线图的不同区间使用不同颜色

R - ggplot2 Legend没有出现在折线图上[重复]

如何用R画折线图,散点图,平滑曲线图

需要帮助使用 ggplot 绘制带有五条线的折线图