In the previous exercises, we added geoms to the plot and explored the idea of layers inheriting the original aesthetic mappings of the canvas. Sometimes, you’ll want individual layers to have their own mappings. For example, what if we wanted the scatterplot layer to classify the points based on a data-driven property? We achieve this by providing an aesthetic mapping for that layer only.
Let’s explore the aesthetic mappings for the geom_point()
layer. What if we wanted to color-code the points on the scatterplot based on a property? It’s possible to customize the color by passing in an aes()
aesthetic mapping with the color based on a data-driven property. Observe this example:
viz <- ggplot(data=airquality, aes(x=Ozone, y=Temp)) + geom_point(aes(color=Month)) + geom_smooth()
The code above would only change the color of the point layer, it would not affect the color of the smooth layer since the aes()
aesthetic mapping is passed at the point layer.
Note: You can read about the individual aesthetics available for each geom when you read its documentation. There are some aesthetics shared across geoms and others that are specific to a particular ones.
Instructions
Inside our movies
dataset, we have a column named nrOfGenre
that describes the number of genres a movie is assigned. For example, the movie “Terminator” is classified as both “Action” and “Sci-Fi”, so its number of genres is equal to 2. What if we are wondering if the number of genres a movie is assigned, in other words its versatility, is correlated to its movie rating or its number of wins? Are movies better off when they stick to one simple genre or when the explore multiple ones? We want to display this information on our plot.
- Add an aesthetic mapping to the
geom_point()
layer thatcolor
coordinates the data based onnrOfGenre
.