- R Data Visualization Recipes
- Vitor Bianchi Lanzetta
- 461字
- 2021-07-02 23:33:33
How it works...
In step 1, calling car::Salaries as the data argument avoids the need to load the whole package. Having the car package installed is a requirement though. Object named box1 stores base ggplot. Proximate line is stacking geom_boxplot() function to box1; therefore adding the box plot geometry. It introduces a new argument, outlier.alpha, responsible for deploying alpha blending to the outliers only.
The outliers can be a source of overplotting when it comes to box plots; adding alpha blending to them is a good practice. There are several other features that can be deployed to outliers only and all of them follow the point.separeted naming convention. Try setting the argument outlier.shape = 3 as an example; it has to be set into geom_boxplot().
You can flip any ggplot by calling coord_flip(). It's a very useful function to flip the box and violin plots.
Next step teaches how to draw similar box plot using plotly. Two very important arguments are type and marker. First one was set to 'box'; hence picking the box plot geometry. Second argument added alpha blending to outliers, notice that it had a list inputted and alpha argument is now called opacity.
This result is now interactive. Besides the compatibility with web applications, it also displays accurate information when mouse is hovered over graphical elements. Step 3 finally draws a similar box plot using ggvis. This was done a little bit differently than the regular way so alpha blending could affect outliers only.
It starts by assigning basic aesthetics mapping to a ggvis object called box3. It has the data frame from which the plot will be drawn on data argument, plus x and y variables. Afterwards, function layer_boxplots() is stacked twice with box3. The first functions adds boxes with alpha (opacity := .4), second one is drawing only the boxes.
Problem is that the whole box receives the alpha to, hence resulting in a weird visual. A solution is to add another box plot overlaying this one, but now erasing the outliers. To proceed this way set argument size := 0. Now we have got ourselves a ggvis box plot which alpha blending seems to affect only outliers.
If you happen not to care about the outliers, you can call:
> box3 %>% layer_boxplots()
Now that you know the very basic of drawing box plots, let's see how to improve the ones coming from ggplot2. When it comes to displaying distribution information, box plots are commonly labeled as simple visualizations. This is not a problem but a characteristic, yet box plots can be worked to display additional information. Next recipe shows how to add notches and alternatively apply jitters to outliers.