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:
-
First and foremost, we’ve switched from using the
qplot()
function to theggplot()
function. Theggplot()
function offers a lot more flexibility and, while it’s generally similar, it also has some significant differences compared toqplot()
. The most crucial difference is that we no longer specify thegeom
in the first function. Instead, we set a separategeom
function, which we add to the original function. We’ve already seen this when we added thegeom_smooth()
function to our plot earlier. So, if we don’t specify the geom, what do we do in the firstggplot()
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. -
A second thing to know is ...