首页 > 解决方案 > ggplot:as.Date.numeric(value) 中的错误:必须提供“原点”

问题描述

我有一个数据集,我正在尝试使用ggplotand来绘制它geofacet。以下是我的数据示例:

# A tibble: 6 x 3
  date       actuals_vol geo  
  <date>           <dbl> <chr>
1 2014-02-02    9607815. AK   
2 2014-02-09    9552680. AK   
3 2014-02-16    9251645. AK   
4 2014-02-23    9177905  AK   
5 2014-03-02    9254805  AK   
6 2014-03-09    9222940. AK   

这是我用来在瓦片地图中绘制数据的代码:

ggplot(cig_prophet_states, aes(date, actuals_vol)) +
  geom_area(fill='#114365') +
  facet_geo(~ geo, grid = "us_state_grid2", label="name") +
  scale_x_continuous(labels = function(x) paste0("'", substr(x, 3, 4))) +
  labs(title = "GDP by State 1997-2016 ($, trillions)",
       caption = "Data Source: St. Louis FRED",
       x = "Year",
       y = "volume") +
  theme(strip.text.x = element_text(size = 8))

我收到以下错误:Error in as.Date.numeric(value) : 'origin' must be supplied

标签: rggplot2

解决方案


只需使用scale_x_date而不是scale_x_continuous

ggplot(cig_prophet_states, aes(date, actuals_vol)) +
  geom_area(fill='#114365') +
  facet_geo(~ geo, grid = "us_state_grid2", label="name") +
  scale_x_date(labels = function(x) paste0("'", substr(x, 3, 4))) +
  labs(title = "GDP by State 1997-2016 ($, trillions)",
       caption = "Data Source: St. Louis FRED",
       x = "Year",
       y = "volume") +
  theme(strip.text.x = element_text(size = 8))

推荐阅读