From the course: Data Visualization in R with ggplot2
Unlock this course with a free trial
Join today to access over 25,100 courses taught by industry experts.
Cleaning up legends
From the course: Data Visualization in R with ggplot2
Cleaning up legends
- [Instructor] Some scales produce visualizations with meanings that aren't obvious to the viewer. Legends can help us clarify these visualizations. A legend provides the viewer with a key to interpreting the scale. On this bar graph, the meaning of the X and y values are obvious from their context. The X axis shows region and the Y axis shows the number of institutions. However, color isn't meaningful by itself. But if we add a legend that explains the colors, we immediately grasp their purpose. The guide_legend function allows us to display legends for scales that don't have intuitive meanings. We commonly use legends for scale, such as fill, color, and shape. Let's give it a try in Rstudio. Here's the code to produce our bar graph of schools. And when I ran this code, R automatically understood that a legend was necessary to explain color, so it added one. It uses some default information. The variable that I mapped to fill was called control so that becomes the title of the legend…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.