Realization of Chart nesting with R language Visualization
This article mainly introduces "R language visualization to achieve chart nesting". In daily operation, I believe that many people have doubts about the realization of chart nesting in R language visualization. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "R language visualization to realize chart nesting"! Next, please follow the editor to study!
There has always been a confusion when I was learning ggplot.
This is whether this function allows two related charts to overlap and nest (that is, to place a smaller version of a smaller image at the edge of a large image (main image)).
It was a strange idea, and I thought it was hopeless, and given the sharp aesthetics of the developer of the package, I guess it can't be allowed to happen.
However, recently, I visited a great god's blog and found a solution to this situation. I was overjoyed and hastened to share it with you here.
However, his approach is not through the built-in function of ggplot, but through the viewport function in the grid package:
The following are the specific implementation steps:
Load package:
Library (ggplot2) # is used for drawing, and both the main and accompanying drawings use ggplot's built-in dataset
Library (grid) # is used to set the length and width of the attached drawings and to stack them in the exact position of the main drawing.
Load and preview the dataset:
Here we still use the data set about diamonds (this data set is used in many previous chart cases).
Data (diamonds)
Head (diamonds)
# make the main image of the composite image:
Chart1