Multiple Vector Plot + line colours and marking

Here is one out of infinitely ways of doing this in ggplot2 Tweaking is required to get the legend box placement entirely to fit on any given plot device.

suppressPackageStartupMessages(library())
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(magrittr))
suppressPackageStartupMessages(library(scales))
suppressPackageStartupMessages(library(tidyr))

# create varible for x-axis
timed <- seq(from = 0.5, to = 1.5, by = 0.2)
# variables from the last post
x1 =  c(0.8262858, 0.9102857, 0.9032932, 0.8994474, 0.8994474)
x2 =  c(0.8151768, 0.9053250, 0.8994474, 0.8975376, 0.8975376)
a =  c(0.3000000, 0.8151768, 0.8151768, 0.8151768, 0.8151768, 0.8151768)
b = c(0.9500000, 0.9500000, 0.9102857, 0.8994474, 0.8994474, 0.8994474)

# create a data frame, ignoring warning about unequal lengths
# x1 and x2 will recycle first value to fill in the missing values
# timed, instead of time, to avoid namespace collision
as.data.frame(cbind(timed,x1,x2,a,b))  -> obj
#> Warning in cbind(timed, x1, x2, a, b): number of rows of result is not a
#> multiple of vector length (arg 2)

# manual kludge
obj[[2]][6] <- NA
obj[[3]][6] <- NA

# pivot_longer to facilitate plotting
jbo <- pivot_longer(obj, c(x1, x2, a, b))

# create the base ggplot object
p <- ggplot(jbo, aes(x = timed, y = value, color = name))
p


# add lines and points to create plot pre embellishments
# points could be shaped or colored, but overlap, which
# would require them to be jittered away from their lines

p + geom_line() + geom_point()
#> Warning: Removed 2 row(s) containing missing values (geom_path).
#> Warning: Removed 2 rows containing missing values (geom_point).


# set line colors and label order
p + geom_line() +
  scale_color_manual(values = c("blue","red","green","lawngreen"),
                     limits = c("x1","x2","a","b"), name = "")
#> Warning: Removed 2 row(s) containing missing values (geom_path).

# position and embellish legend
p + geom_line() +
  scale_color_manual(values = c("blue","red","green","lawngreen"),
                     limits = c("x1","x2","a","b"), name = "") +
  theme(legend.position = c(.97,.123),
      legend.background = element_rect(fill = "white", color = "black"),
)
#> Warning: Removed 2 row(s) containing missing values (geom_path).


# set scales for axes
p + geom_line() +
  scale_x_continuous(breaks = c(0.5,0.6,0.8,1.0,1.2,1.4,1.5)) +
  scale_y_continuous(breaks = c(0.3,0.8,0.82,0.9,0.95))  +
  scale_color_manual(values = c("blue","red","green","lawngreen"),
                     limits = c("x1","x2","a","b"), name = "") +
  theme(legend.position = c(.97,.123),
        legend.background = element_rect(fill = "white", color = "black"),
  )
#> Warning: Removed 2 row(s) containing missing values (geom_path).


# set panel background to white with gray grid lines with black border

p + geom_line() + geom_point() +
  scale_x_continuous(breaks = c(0.5,0.6,0.8,1.0,1.2,1.4,1.5)) +
  scale_y_continuous(breaks = c(0.3,0.8,0.82,0.9,0.95))  +
  scale_color_manual(values = c("blue","red","green","lawngreen"),
                     limits = c("x1","x2","a","b"), name = "") +
  theme(legend.position = c(.97,.123),
        legend.background = element_rect(fill = "white", color = "black"),
        panel.background = element_rect(fill = "white"),
        panel.grid.major = element_line(color = "gray"),
        panel.grid.minor = element_line(colour = "lightgray"),
        panel.border = element_rect(colour = "black", fill = NA, size = 2.75)
  )
#> Warning: Removed 2 row(s) containing missing values (geom_path).

#> Warning: Removed 2 rows containing missing values (geom_point).


# set labels
x_lab <-  "Time Period (t)"
y_lab <-  "Threshold/Effort Level"
headline <- "Evolution of Legal Threshold and Effort Levels"
subdeck <- "Prepared by ____________, 2020-03-20"
p + geom_line() + geom_point() +
  scale_x_continuous(breaks = c(0.5,0.6,0.8,1.0,1.2,1.4,1.5)) +
  scale_y_continuous(breaks = c(0.3,0.8,0.82,0.9,0.95))  +
  scale_color_manual(values = c("blue","red","green","lawngreen"),
                     limits = c("x1","x2","a","b"), name = "") +
  theme(legend.position = c(.97,.123),
        legend.background = element_rect(fill = "white", color = "black"),
        panel.background = element_rect(fill = "white"),
        panel.grid.major = element_line(color = "gray"),
        panel.grid.minor = element_line(colour = "lightgray"),
        panel.border = element_rect(colour = "black", fill = NA, size = 2.75)
  ) +
  labs(title = headline, subtitle = subdeck) + xlab(x_lab) +  ylab(y_lab)
#> Warning: Removed 2 row(s) containing missing values (geom_path).

#> Warning: Removed 2 rows containing missing values (geom_point).

Created on 2020-03-20 by the reprex package (v0.3.0)

2 Likes