首页 > 解决方案 > ggplot 仅显示 geom_line() 时间序列中的一些数据

问题描述

我正在使用此处找到的公开可用的天然气价格数据来创建时间序列图,但 ggplot 似乎只显示 1 月份每年点的数据,而没有其他数据。谁能帮我确定为什么会这样?

我正在使用以下代码:

library(readxl)
library(tidyverse)
library(tidyr)
library(scales)
library(extrafont)
library(lubridate)
raw_data <- read.csv("H:/Projects/gas prices2.csv") %>%
  mutate(date2 = as.Date(dmy(date)))

frmt_data <- subset(raw_data, date2 >= as.Date("2012-01-01")) %>% 
  select(-date)

class(frmt_data$date2)
ggplot(frmt_data, aes(x = date2, y = price))+
  labs(x = "", y = "Dollars per Gallon ($)", caption = "SOURCE: U.S. Energy Information Administration") +
  annotate(geom = "segment", x = as.Date("2017-01-20"),
           xend = as.Date("2017-01-20"),
           y = 0, yend = 3.5, color = "#D5433E", size = 1) +
  annotate(geom = "text", x = as.Date("2017-01-20"),
           y = 3.6, color = "black", size = 2.8, hjust = .5,
           label = expression("Jan 20: Trump\ntook Office")) + 
  annotate(geom = "segment", x = as.Date("2021-01-20"),
           xend = as.Date("2021-01-20"),
           y = 0, yend = 3.5, color = "#D5433E", size = 1) +
  annotate(geom = "text", x = as.Date("2021-01-20"),
           y = 3.6, color = "black", size = 2.8, hjust = .5,
           label = expression("Jan 20: Biden\ntook Office")) +
  annotate(geom = "segment", x = as.Date("2020-03-11"),
           xend = as.Date("2020-03-11"),
           y = 0, yend = 3, color = "#D5433E", size = 1) +
  annotate(geom = "text", x = as.Date("2020-03-01"),
           y = 3.1, color = "black", size = 2.8, hjust = .5,
           label = expression("COVID hits")) +
  geom_line(size = 1.2, lineend = "round", color = "#61D4D2") +
  scale_y_continuous(labels = dollar_format()) +
  scale_x_date(date_labels = "%b- %Y", date_breaks = "6 months") +
  guides(color = guide_legend(override.aes = list(size = 1.5), nrow = 1))+
  theme(
    text=element_text(family="Roboto Light"),
    legend.position = "bottom",
    legend.title = element_blank(),
    legend.key=element_blank(),
    legend.text = element_text(size = 13, family = "Roboto Light"),
    panel.background = element_rect(fill = "transparent"),
    plot.title = element_text(family = "Roboto", hjust = .5, vjust = 2.5),
    plot.background = element_rect(fill = "transparent"),
    plot.margin = unit(c(0, 1.5, 0, 0), "cm"),
    panel.grid.major.x = element_line(size=.1, color="light grey"),
    axis.text = element_text(size = 11, family = "Roboto Medium"),
    axis.text.x = element_text(angle = 45, vjust = 0.5, size = 9),
    axis.title.y = element_text(size = 13, margin = margin(t = 0, r = 6, b = 0, l = 0)),
    axis.line.x=element_line(),
    axis.line.y=element_line()
    )

生成的图像

标签: rggplot2

解决方案


我不确定您的代码到底有什么问题 - 我想说日期值的格式发生了一些事情。在下载每周数据并将其转换为 csv 后,我能够生成您的图形。

library(tidyverse)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

raw_data <- read_csv("~/Downloads/EMM_EPM0_PTE_NUS_DPGw.csv", skip =2)
#> 
#> ── Column specification ────────────────────────────────────────────────────────
#> cols(
#>   Date = col_character(),
#>   `Weekly U.S. All Grades All Formulations Retail Gasoline Prices  (Dollars per Gallon)` = col_double()
#> )

transmute(raw_data, Date = mdy(Date), 
          price = `Weekly U.S. All Grades All Formulations Retail Gasoline Prices  (Dollars per Gallon)`) %>%
  filter(Date >= ymd("2012-01-01")) %>%
  ggplot(aes(Date, price)) +
  geom_line(color = "#61D4D2") +
  theme_classic() +
  scale_x_date(date_labels = "%b- %Y", date_breaks = "6 months") +
  scale_y_continuous(labels = scales::dollar_format(), expand = expansion(c(0,.1))) +
  annotate(geom = "segment", x = as.Date("2017-01-20"),
           xend = as.Date("2017-01-20"),
           y = 0, yend = 3.5, color = "#D5433E", size = 1) +
  annotate(geom = "text", x = as.Date("2017-01-20"),
           y = 3.6, color = "black", size = 2.8, hjust = .5,
           label = expression("Jan 20: Trump\ntook Office")) + 
  annotate(geom = "segment", x = as.Date("2021-01-20"),
           xend = as.Date("2021-01-20"),
           y = 0, yend = 3.5, color = "#D5433E", size = 1) +
  annotate(geom = "text", x = as.Date("2021-01-20"),
           y = 3.6, color = "black", size = 2.8, hjust = .5,
           label = expression("Jan 20: Biden\ntook Office")) +
  annotate(geom = "segment", x = as.Date("2020-03-11"),
           xend = as.Date("2020-03-11"),
           y = 0, yend = 3, color = "#D5433E", size = 1) +
  annotate(geom = "text", x = as.Date("2020-03-01"),
           y = 3.1, color = "black", size = 2.8, hjust = .5,
           label = expression("COVID hits")) +
  theme(
    legend.position = "bottom",
    legend.title = element_blank(),
    legend.key=element_blank(),
    panel.background = element_rect(fill = "transparent"),
    plot.title = element_text(family = "Roboto", hjust = .5, vjust = 2.5),
    plot.background = element_rect(fill = "transparent"),
    plot.margin = unit(c(0, 1.5, 0, 0), "cm"),
    panel.grid.major.x = element_line(size=.1, color="light grey"),
    axis.text.x = element_text(angle = 45, vjust = 0.5, size = 9),
    axis.title.y = element_text(size = 13, margin = margin(t = 0, r = 6, b = 0, l = 0)),
    axis.line.x=element_line(),
    axis.line.y=element_line()
  )
#> Warning in is.na(x): is.na() applied to non-(list or vector) of type
#> 'expression'

#> Warning in is.na(x): is.na() applied to non-(list or vector) of type
#> 'expression'

#> Warning in is.na(x): is.na() applied to non-(list or vector) of type
#> 'expression'

reprex 包于 2021-06-17 创建 (v2.0.0 )


推荐阅读