Time Series Graphs with Different Symbols

Viewed 52

I randomly simulated this time series data:

n_obs = 1:100

a1 = rexp(100,5)
b1 = rexp(100,5)

a1_col = "red"
b1_col = "blue"

symbol <- c("square","triangle")

point_symbol <- sample(symbol, 100, replace=TRUE, prob=c(0.5, 0.5))

stock_data = data.frame(n_obs, a1, b1, a1_col, b1_col, point_symbol)

stock_data$point_symbol_color = ifelse(stock_data$a1 > stock_data$b1, "hollow", "dark")

I was able to make a graph of this data:

library(ggplot2)

ggplot() + 
  geom_line(data = stock_data, aes(x = n_obs, y = a1), color = "red") +
  geom_line(data = stock_data, aes(x = n_obs, y = b1), color = "blue") +
  xlab('n_obs') +
  ylab('exp value') + ggtitle("sample")

enter image description here

  • Is it possible to change the "shape" of each point according to the symbols I indicated above? I think this can be done with the following commands (https://www.datanovia.com/en/blog/ggplot-point-shapes-best-tips/) - but I am not sure how to add it to my existing plot:

  • Is it possible to change the "shading/fill" of the squares/triangles based on the "hollow"/"dark" values I have specified?

I think this can be done like this (I couldn't find the exact "hollow"/"dark" values here:https://ggplot2.tidyverse.org/reference/geom_point.html)

geom_point(shape = ifelse(stock_data$point_symbol == "square", 0,2) , fill = ifelse(stock_data$point_symbol_color == "hollow", "hollow", "dark"), color = "black", size = 3) - but I am not sure how to add this to the plot I made.

Is it also possible to add a legend to this plot?

  • "a red line" = a1
  • "a blue line" = b1
  • "a square symbol" = square
  • "a triangle symbol" = triangle

Thank you!

1 Answers

It is easiest to work with ggplot when your data is in a "long format", i.e. each row has one X value and one Y value. The tidyr package has the handy pivot_longer() function to do just that.

Also in need to change your plot symbol from "hollow"/"dark" to "open"/"filled"

All the scale_*_identity() are used to use the values stored in the data frame and not the default values.

n_obs = 1:100

a1 = rexp(100,5)
b1 = rexp(100,5)
symbol <- c("square","triangle")  #define the shape
point_symbol <- sample(symbol, 100, replace=TRUE, prob=c(0.5, 0.5))


stock_data = data.frame(n_obs, a1, b1, point_symbol)

stock_data$point_symbol_color = ifelse(stock_data$a1 > stock_data$b1, "open", "filled")


library(tidyr)
#convert the data frame to long format
stock_long <- pivot_longer(stock_data, cols=c("a1", "b1"), names_to = "stock", values_to = "value")
#add color definition
stock_long$color <- ifelse(stock_long$stock=="a1", "red", "blue")

library(ggplot2)
ggplot(stock_long, aes(x=n_obs, y=value, group=stock, color=color)) +
   geom_line() +
   geom_point(aes(shape=paste(point_symbol, point_symbol_color), fill=color)) +
   scale_color_identity("Stock", labels=c("B1", "A1"), guide="legend") +
   scale_shape_identity() +
   scale_fill_identity()

enter image description here

Related