Lecture 26

Time Trend ggplot()

Byeong-Hak Choe

SUNY Geneseo

November 6, 2024

Time Trend ggplot()

Time Trend ggplot()

NVDA Stock Price

  • The nvda data.frame includes NVIDIA’s stock information from 2019-01-02 to 2024-10-18.

Time Trend ggplot()

Scatterplot for Time Trend?

path <- 
  "https://bcdanl.github.io/data/nvda_2015_2024.csv"
nvda <- read_csv(path)

ggplot( data = nvda,
        mapping = aes(
          x = Date, 
          y = Close) ) + 
  geom_point(size = .5)

Time Trend ggplot()

Line Chart with geom_line()

path <- 
  "https://bcdanl.github.io/data/nvda_2015_2024.csv"
nvda <- read_csv(path)

ggplot( data = nvda,
        mapping = aes(
          x = Date, 
          y = Close) ) + 
  geom_point(size = .5) +
  geom_line()

  • geom_line() draws a line by connecting data points in order of the variable on the x-axis.

Time Trend ggplot()

The Connection Principle

  • We tend to think of objects that are physically connected as part of a group.
  • Look at this figure.
    • Your eyes probably pair the shapes connected by lines rather than similar color, size, or shape!
  • We frequently leverage the connection principle is in line charts, to help our eyes see order in the data.

Time Trend ggplot()

Line Chart with geom_line() and geom_smooth()

path <- 
  "https://bcdanl.github.io/data/nvda_2015_2024.csv"
nvda <- read_csv(path)

ggplot( data = nvda,
        mapping = aes(
          x = Date, 
          y = Close) ) + 
  geom_point(size = .5) +
  geom_line() +
  geom_smooth()

  • geom_smooth() can also be useful for illustrating overall time trends.

Time Trend ggplot()

Tech Stocks’ Prices in October

  • The tech_october data.frame includes stock information about AAPL, NVDA, META, and TSLA in October 2024.

Time Trend ggplot()

Tech Stock Price

tech_october <- 
  read_csv(
    "https://bcdanl.github.io/data/tech_stocks_2024_10.csv"
    )

ggplot( data = tech_october,
        mapping = aes(
          x = Date, 
          y = Close) ) + 
  geom_line() 

  • Something has gone wrong. What happened?

Time Trend ggplot()

Tech Stock Price

# `ggplot` needs to be 
# explicitly informed that 
# daily observations are grouped 
# by `Ticker`
# for it to understand 
# the grouping structure

ggplot( data = tech_october,
        mapping = aes(
          x = Date, 
          y = Close,
          color = Ticker) ) + 
  geom_line(size = 2) # thicker lines

  • We can use either group, color, or linetype aesthetic to tell ggplot explicitly about this firm-level structure.