Visualizing aspects of data with facets
1. Visualizing aspects of data with facets
Welcome to the second chapter of this course. In this chapter, you are going to explore a less conventional form of visualization, the dot plot. But before that, we are going to quickly revise faceting – a nice ggplot-way of looking at different aspects of a data set.2. The facet_grid() function
You may already know about the facet_wrap function from previous courses. Here, you'll learn about the facet_grid function. Its advantage is that you can determine whether you want to order the facets horizontally or vertically. For that, the arguments given to the function are slightly different than for facet_wrap. By using the rows or cols named argument together with a variable quoted by the vars function, you can explicitly tell ggplot to arrange the plot vertically or horizontally, respectively.3. The facet_grid() function
Actually, the facet_grid call with cols = vars(year) gives identical results as facet_wrap with the same argument value. Notice, however, that facet_grid allows to customize whether you want the plots to be arranged in horizontal or vertical order, which is not possible with facet_wrap.4. A faceted scatter plot
In the last exercise, you generated this nice-looking scatter plot. It shows us the relationship in the year 2006. In the histograms for both years we saw that the weekly working hours have been decreasing overall. That means, in 1996 there were more countries with higher working hours than in 2006. But what happens to the relationship between working hours and hourly compensation? You're going to explore this in the exercises, where you will facet this scatter plot, so there are two different facets for both the years.5. Styling faceted plots
Another reason we are revisiting faceting here is that there are some specific theme options for faceted plots, like strip-dot-background and strip-dot-text. They can be used to style the small titles that are given to each facet, here displaying the respective year.6. Defining your own theme function
Another helpful thing you are going to try out in the exercises is defining your own theme function. Basically, a function definition works like this: You define a new variable, the name of the function. This name, in this case theme_green, is followed by the assignment operator and the function keyword, which is then followed by parentheses and an opening curly bracket. After that, the so-called function body defines what's happening once this function is executed. Here, you just call a normal ggplot2 theme function with your own custom theme options. You then close the function body with a curly bracket. The cool thing about a custom theme function is that you can then apply it to any plot object you like. If we apply our new theme_green-function to the histogram, it quickly changes its appearance. Well, this plot certainly doesn't win a beauty contest, but you get the idea. You're going to try this out in the exercises.7. Let's practice!
Let's do it!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.