We will continue working with Lattice and see some more things that are possible to do with lattice. The answers to these exercises are available here. You can also check the previous before diving into it.
Create a Box whisker plot from the diamonds data-set, where I want to see the price’s distribution (ex. box-whisker.) This is ordered by mean of the price of each cut, which means the cut, which has the highest mean price, should appear on top and so on. Make sure it shows the extreme outliers for each category.
You can easily see two or more variable’s distribution in a single plot with lattice. Create a density plot of Sepal.Length and Sepal.Width in a single plot.
Now, on the same plot, suppress plotting the points individually.
Now we will introduce how to encode categorical variables with colors. Plot Sepal.Length vs. Sepal.Width as a scatter plot with each species colored differently.
How do you achieve that?
Adding color based groups to visualizations means we need to add legends to the plot, so as to link colors to the factors. Add a legend the to the plot above. It is pretty simple in lattice; also, add a label to the x-axis.
It is also possible to customize the legend based on our need. By default,the legend appears on top of the plot. Move the legend to the right and give it a meaningful name. This neat trick allows the visualization to be understood better.
Now draw a scatter plot of Sepal.Width vs. Sepal.Length and separate the plots (small multiples like the last exercise set) for different species. Make sure you leave a space between them so that we can clearly distinguish 3 of the plots by a cursory look.
On the same plot, add a reference line so that the line has 2 intercepts and 1 slope.
It is possible to customize the small panels name; sometimes it is necessary because you want the names to be meaningful. For this exercise, use the same plot as above, but rename the panel’s name appearing “abobe” with Setosa as Set, Versicolor as Ver, Virginica as Vir (This does not make much sense but this will introduce the concept.)
Tip: use strip and strip.custom.
By default, lattice draws conditional plots starting from the bottom left. We can change that behavior by ordering them from top left. Draw a scatter plot of carat vs. price in diamonds data for each clarity level. Now, draw it again so that the order is from top left. Tip – Use as.table parameter.