【问题标题】:Unable to add legend to economic time series chart无法将图例添加到经济时间序列图表
【发布时间】:2019-05-17 13:38:55
【问题描述】:

我正在尝试将图例添加到时间序列图表中,但到目前为止我无法获得任何牵引力。我在下面提供了工作代码,它将三个经济数据系列拉到一个图表中,并应用了一些更改来获得我想要的格式/整体美感。我还应该补充一点,该图表正在绘制季度数据集的 y/y 变化。

我只能找到使用 scale_colour_manual 添加图例的个人示例 - 我在下面提供了代码。

理想情况下,图例只需要出现在带有颜色和折线图的图表的右侧。

任何帮助将不胜感激!

library(quantmod)
library(TTR)
library(ggthemes)
library(tidyverse)

Nondurable <- getSymbols("PCND", src = "FRED", auto.assign = F)
Nondurable$chng <- ROC(Nondurable$PCND,4)
Durable <- getSymbols("PCDG", src = "FRED", auto.assign = F)
Durable$chng <- ROC(Durable$PCDG,4)
Services <- getSymbols("PCESV", src = "FRED", auto.assign = F)
Services$chng <- ROC(Services$PCESV, 4)

ggplot() +
  geom_line(data = Nondurable, aes(x = Index, y = chng), color = "#5b9bd5", size = 1, linetype = "solid") +
  geom_line(data = Durable, aes(x = Index, y = chng), color = "#00b050", size = 1, linetype = "longdash") +
  geom_line(data = Services, aes(x = Index, y = chng), color = "#ed7d31", size = 1, linetype = "twodash") +
  theme_tufte() + 
  scale_y_continuous(labels = percent, limits = c(-0.01,.09)) +
  xlim(as.Date(c('1/1/2010', '6/30/2019'), format="%d/%m/%Y")) + 
  labs(y = "Percent Change", x = "", caption = "Seasonally Adjusted Annual Rate. Retrieved from FRED & U.S. Bureau of Economic Analysis") +
  ggtitle("Year-over-Year Spending Trend Changes of the US Consumer") +
  scale_colour_manual(name = 'Legend', 
                      guide = 'legend',
                      values = c('Nondurable' = '#5b9bd5',
                                 'Durable' = '#00b050',
                                 'Services' = '#ed7d31'), 
                      labels = c('Nondurable',
                                 'Durable',
                                 'Services'))

我在运行程序时收到以下警告消息(但图表仍在绘制)。

Warning messages:
1: Removed 252 rows containing missing values (geom_path). 
2: Removed 252 rows containing missing values (geom_path). 
3: Removed 252 rows containing missing values (geom_path).

【问题讨论】:

  • 对我来说,它会抛出 check_breaks_labels(breaks, labels) 中的错误:找不到对象“百分比”
  • 奇怪 - 我已经编辑了我的代码以包含现在加载的所有包。
  • 谢谢,我已经加载了quantmodggplot2ggthemes。缺少的是scales
  • 您是否在代码中看到任何会导致 scale_color_manual 不投射图例的错误?
  • 不,我尝试了几种方法,但都没有奏效。明天我会回到这个。

标签: r ggplot2 quantmod geom-text economics


【解决方案1】:

您收到此错误的原因有两个:

  1. 由于您的限制,正在删除批量。当您使用xlim()scale_y_continuous(..., limits = ...) 时,ggplot 会在绘图之前从您的数据中删除超出这些限制的值,并将该警告显示为仅供参考。在注释掉这两行之后,您仍然会看到一条关于已删除值的消息,但数字要小得多。这是因为
  2. chng 列的前 4 行中有 NA 值。这在所有 3 个数据集中都是如此。

为了显示比例,您需要在aes() 中添加一些区分线条的内容,如aes(..., color = "Nondurable") 中一样。看看这个解决方案是否适合你:

ggplot() +
  geom_line(data = Nondurable, aes(x = Index, y = chng, color = "Nondurable"), size = 1, linetype = "solid") +
  geom_line(data = Durable, aes(x = Index, y = chng, color = "Durable"), size = 1, linetype = "longdash") +
  geom_line(data = Services, aes(x = Index, y = chng, color = "Services"), size = 1, linetype = "twodash") +
  theme_tufte() +
  labs(
    y = "Percent Change", 
    x = "", 
    caption = "Seasonally Adjusted Annual Rate. Retrieved from FRED & U.S. Bureau of Economic Analysis"
  ) +
  ggtitle("Year-over-Year Spending Trend Changes of the US Consumer") +
  scale_colour_manual(
    name = "Legend",
    values = c("#5b9bd5","#00b050","#ed7d31"),
    labels = c("Nondurable", "Durable", "Services"
    )
  ) +
  scale_x_date(limits = as.Date(c("2010-01-01", "2019-02-01")))

【讨论】:

  • 我已经删除了 xlim(0)scale_y_continuous() 并且图例仍然没有出现。注意 - 没有添加任何新包。
  • 一段时间后我回到这个问题并注意到一个问题......如果我们删除 xlim()...... 调整 X 轴开始的最佳方法是什么...... .2010 年。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-05
  • 1970-01-01
  • 2013-01-28
  • 2016-12-16
  • 2013-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多