Skip to content

Latest commit

 

History

History
78 lines (73 loc) · 1.41 KB

[M]-(Time-Trend)-Line-Graph_Non-time-series.md

File metadata and controls

78 lines (73 loc) · 1.41 KB

[Time Trend][Non-time-series] Line Graph

Base R Graphic

Single Line
Preparation Code
# Sample Data
library(wooldridge)
B <- barium
Actual Code
plot(B$t, B$gas,
     main = "Line Graph of Gasoline Production Over Time",
     ylab = "Gasoline Production", xlab = "Time Trend", type = "n")
lines(B$t, B$gas)

Multiple Lines
Preparation Code
# Sample Data
library(dplyr)
library(wooldridge)
P <- prminwge
Actual Code
plot(P$prgnp,
     ylim = c(0, 5000),
     main = "Line Graph of Gross National Product over Time",
     ylab = "Gross National Product", xlab = "Time Trend", type = "n")
lines(P$prgnp, col = "lightblue")  
lines(P$usgnp, col = "pink")
legend("topleft",
       legend = c("Puerto Rico", "United States"),
       col = c("lightblue", "pink"),
       lty = 1,
       cex = 0.8)

ggplot2 Graphic

Single Line
Preparation Code
# Functions
library(ggplot2)

# Sample Data
library(wooldridge)
B <- barium
Actual Code
ggplot(B, aes(x = t, y = gas)) +
  geom_line()

Multiple Lines
Preparation Code
# Functions
library(ggplot2)

# Sample Data
library(dplyr)
library(babynames)
B <- babynames %>% 
  filter(name %in% c("Ashley", "Patricia", "Helen")) %>%
  filter(sex=="F")
Actual Code
ggplot(B, aes(x=year, y=n, group=name, color=name)) +
    geom_line()