ggplot2折线图

R语言
可视化
ggplot2折线图
作者

不止BI

发布于

2023年10月15日

ggplot2折线图汇总

示例数据

library(tidyverse)
sp500_data_wide <- gt::sp500 |>
  select(date, open, close) |>
  filter(year(date) == 2014, month(date) == 1)
sp500_data <- sp500_data_wide %>%
  pivot_longer(
    cols = -date,
    names_to = "type",
    values_to = "price"
  )
sp500_data
# A tibble: 42 × 3
   date       type  price
   <date>     <chr> <dbl>
 1 2014-01-31 open  1791.
 2 2014-01-31 close 1783.
 3 2014-01-30 open  1777.
 4 2014-01-30 close 1794.
 5 2014-01-29 open  1790.
 6 2014-01-29 close 1774.
 7 2014-01-28 open  1783 
 8 2014-01-28 close 1792.
 9 2014-01-27 open  1791.
10 2014-01-27 close 1782.
# ℹ 32 more rows

绘制折线图

基本折线图

sp500_data %>%
  ggplot(mapping = aes(x = date, y = price, color = type)) +
  geom_line(linewidth = 1.25)

使用主题

sp500_data %>%
  ggplot(mapping = aes(x = date, y = price, color = type)) +
  geom_line(linewidth = 1.25) +
  theme_minimal(
    base_size = 20
  ) +
  theme(
    panel.grid.minor = element_blank()
  )

修改标题及标签

sp500_data |>
  ggplot(aes(date, price, col = type)) +
  geom_line(linewidth = 1.25) +
  theme_minimal(
    base_size = 20
  ) +
  theme(
    panel.grid.minor = element_blank()
  ) +
  scale_color_manual(
    values = c("#0072B2", "#D55E00")
  ) +
  labs(
    x = element_blank(),
    y = element_blank(),
    title = "SP500 Prices in January 2014"
  ) +
  scale_y_continuous(
    labels = scales::label_dollar()
  )

增加折线标签

sp500_data_with_nicer_labels <- sp500_data |>
  mutate(
    type = if_else(
      type == "open",
      "Opening price",
      "Closing price"
    )
  )

sp500_data_with_nicer_labels |>
  ggplot(aes(date, price, col = type)) +
  geom_line(linewidth = 1.25) +
  geom_text(
    data = sp500_data_with_nicer_labels |>
      slice_head(n = 1, by = type),
    aes(label = type),
    hjust = 0,
    vjust = 0,
    size = 10,
    nudge_x = 0.1
  ) +
  theme_minimal(
    base_size = 20,
  ) +
  theme(
    panel.grid.minor = element_blank(),
    legend.position = "none"
  ) +
  scale_color_manual(
    values = c("#0072B2", "#D55E00")
  ) +
  labs(
    x = element_blank(),
    y = element_blank(),
    title = "SP500 Prices in January 2014"
  ) +
  scale_y_continuous(
    labels = scales::label_dollar()
  ) +
  scale_x_date(
    limits = c(
      make_date(2014, 1, 1),
      make_date(2014, 2, 8)
    )
  )

添加阴影

sp500_data_with_nicer_labels |>
  ggplot(aes(date, price, col = type)) +
  ggbraid::geom_braid(
    data = sp500_data_wide,
    aes(
      y = NULL, ## Overwrite the inherited aes from ggplot()
      col = NULL,
      ymin = open,
      ymax = close,
      fill = open < close
    ),
    alpha = 0.6
  ) +
  geom_line(linewidth = 1.25) +
  geomtextpath::geom_textline(
    data = sp500_data_with_nicer_labels |>
      filter(type == "Opening price"),
    aes(label = type),
    hjust = 0.76,
    vjust = 0,
    size = 8
  ) +
  geomtextpath::geom_textline(
    data = sp500_data_with_nicer_labels |>
      filter(type == "Closing price"),
    aes(label = type),
    hjust = 0.77,
    vjust = 1,
    size = 8,
    text_smoothing = 40,
    offset = unit(-14, "mm")
  ) +
  theme_minimal(
    base_size = 20,
  ) +
  theme(
    panel.grid.minor = element_blank(),
    legend.position = "none"
  ) +
  scale_color_manual(
    values = c("#0072B2", "#D55E00")
  ) +
  scale_fill_manual(
    values = c("TRUE" = "#0072B2", "FALSE" = "#D55E00")
  ) +
  labs(
    x = element_blank(),
    y = element_blank(),
    title = "SP500 Prices in January 2014"
  ) +
  scale_y_continuous(
    labels = scales::label_dollar()
  )

添加说明

sp500_data_with_nicer_labels |>
  ggplot(aes(date, price, col = type)) +
  ggbraid::geom_braid(
    data = sp500_data_wide,
    aes(
      y = NULL, ## Overwrite the inherited aes from ggplot()
      col = NULL,
      ymin = open,
      ymax = close,
      fill = open < close
    ),
    alpha = 0.6
  ) +
  geom_line(linewidth = 1.25) +
  geomtextpath::geom_textline(
    data = sp500_data_with_nicer_labels |>
      filter(type == "Opening price"),
    aes(label = type),
    hjust = 0.76,
    vjust = 0,
    size = 8
  ) +
  geomtextpath::geom_textline(
    data = sp500_data_with_nicer_labels |>
      filter(type == "Closing price"),
    aes(label = type),
    hjust = 0.77,
    vjust = 1,
    size = 8,
    text_smoothing = 40,
    offset = unit(-14, "mm")
  ) +
  ggforce::geom_mark_circle(
    data = tibble(
      date = make_date(2014, 1, 24),
      price = 1820
    ),
    aes(
      col = NULL,
      label = "This area signals whether the\nclosing price or opening price was\nhigher on a given day"
    ),
    fill = "white",
    color = "grey20",
    alpha = 1,
    x0 = make_date(2014, 1, 13),
    y0 = 1805,
    label.colour = "grey20",
    label.hjust = 0,
    label.fontsize = 12,
    label.fontface = "plain",
    con.colour = "grey20",
    con.cap = unit(1, "mm"),
    expand = 0.011
  ) +
  theme_minimal(
    base_size = 20,
  ) +
  theme(
    panel.grid.minor = element_blank(),
    legend.position = "none"
  ) +
  scale_color_manual(
    values = c("#0072B2", "#D55E00")
  ) +
  scale_fill_manual(
    values = c("TRUE" = "#0072B2", "FALSE" = "#D55E00")
  ) +
  labs(
    x = element_blank(),
    y = element_blank(),
    title = "SP500 Prices in January 2014"
  ) +
  scale_y_continuous(
    labels = scales::label_dollar()
  )

## `geom_braid()` using method = 'line'
回到顶部