Check out example codes for "common legend for multiple plots in r". It will help you in understanding the concepts better.

Code Example 1

plot_colors <- c("blue","black", "green", "orange", "pink")
  text <- c("Fabricated Metal", "Iron and Steel", "Paper", 
  "Beverages", "Tobacco")
  plot.new()
  par(xpd=TRUE)
  legend("center",legend = text, text.width = max(sapply(text, strwidth)),
         col=plot_colors, lwd=5, cex=1, horiz = TRUE)
  par(xpd=FALSE)

Code Example 2

# for ggplot
ggarrange(bxp, dp,  common.legend = TRUE)

Learn ReactJs, React Native from akashmittal.com