...

/

Plotting with ggplot() Instead of qplot()

Plotting with ggplot() Instead of qplot()

Learn to plot with ggplot() instead of the standard library qplot().

Let’s learn about the ggplot() function, which is used to plot data.

Some tips regarding the ggplot() function

Now, we can plot these regressions with their confidence intervals. The following code introduces many new aspects of plotting with ggplot2, so it’s important to walk through it all:

  1. First and foremost, we’ve switched from using the qplot() function to the ggplot() function. The ggplot() function offers a lot more flexibility and, while it’s generally similar, it also has some significant differences compared to qplot(). The most crucial difference is that we no longer specify the geom in the first function. Instead, we set a separate geom function, which we add to the original function. We’ve already seen this when we added the geom_smooth() function to our plot earlier. So, if we don’t specify the geom, what do we do in the first ggplot() function?

    Essentially, we determine what will be used to build the figure. We assign the data, and we give the aesthetics, which is the ggplot2 way of saying that we use the aes() argument/function to set up the data that’s used for the x- and y-axes and to set up if we’re going to color or fill our data by any other variables. Then, we add various functions to make the plot look the way we want. An example of this was made, but we’ll walk through the code much more slowly.

  2. A second thing to know is ...