Create chart with multiple graphs using global data
Introduction
We have already covered how to create basic multiple graph charts here.
The drawback of the previous method is that we were explicitly specifying mapping in each geom function:
… + geom_point(mapping = aes(x = displ, y = hwy)) + geom_smooth(mapping = aes(x = displ, y = hwy))
It will be tedious to change the mapping individually for each geom function. Fortunately, ggplot2 supports global data which will be implicitly used by every geom function.
Procedure
We will be working with the MPG data set present in-built in ggplot2. Few of the rows of the dataset are as follows:
We have already looked at creating a basic multiple graph charts here.
We will now setup a global data which can be used by all geom functions.
Code
We specify the global data in the ggplot(…) function itself.
library(ggplot2)
# multiple geoms in same plot using global data
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() + geom_smooth()
And the output of above code is:
We can explicitly override global data and override global aesthetics also.
Conclusion
Thus we have successfully created multiple graph on same charts using global data.
References
- https://r4ds.had.co.nz/