About

In this article, I demonstrate how to use data in wide format to generate a ggplot with legend. This is not very easy as when there is a grouping column to color and generate legend for the plot.

Getting data with multiple columns

First, we check and confirm that the y-axis variables we want to plot are in different columns within the dataframe. In this case, we will plot disp and hp against mpg.

data(mtcars)
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Generating the plot

Then we can go ahead and plot different columns on one plot and show the legend.

library(ggplot2)
ggplot(data = mtcars, aes(x = mpg)) +
  geom_line(aes(y = disp, col = 'disp')) +
  geom_point(aes(y = disp, col = 'disp')) +
  geom_line(aes(y = hp, col = 'hp')) +
  geom_point(aes(y = hp, col = 'hp')) +
  labs(x = 'Miles per gallon', y = 'Displacement/Hp', title = 'Multiple columns and legend') +
  scale_color_manual(values = c('disp' = 'magenta', 'hp' = 'purple')) +
  theme(text = element_text(size = 20))

References

R Core Team. 2021. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.
Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen, Kohske Takahashi, Claus Wilke, Kara Woo, Hiroaki Yutani, and Dewey Dunnington. 2021. Ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. https://CRAN.R-project.org/package=ggplot2.