concept ggplot in category R

appears as: ggplot, The ggplot, ggplot
R in Action, Third Edition

This is an excerpt from Manning's book R in Action, Third Edition.

Plots created with ggplot2 always start with the ggplot function. In the previous examples, the data= and mapping= options were placed in this function. In this case they apply to each geom function that follows.

Exploring Data with R

This is an excerpt from Manning's book Exploring Data with R.

In the previous chapter, it was proposed that data visualization is a useful process for generating insights. It is often necessary to transform data before it’s ready for plotting, and, the dplyr functions we just learned about make those transformations possible. Now, we will learn about how to create beautiful and informative plots with ggplot (actually called ggplot2 but cited throughout this book more simply as ggplot). While ggplot provides functionality to create a wide selection of plot types, we will begin by focusing on a single type of plot: the scatterplot.

This book’s package, edr, provides the input data we’ll need to build our example plots. From that package the dmd dataset will be used (it’s a simplified version of the diamonds dataset that is available in the ggplot package). Whenever we load in the edr package using library(edr), the dmd table will be available; you can verify this by executing dmd in the R console. This particular dataset has nearly 2,700 rows, one for each diamond and its attributes, and the following 6 variables:

Listing 3.1 Code to make our first ggplot.
```{r gg_empty}
ggplot(data = dmd, mapping = aes(x = carats, y = price)) //#A
```
Figure 3.22. Modifying data with dplyr; plotting it with ggplot.
Listing 3.24 Modifying `dmd` to add three new columns, and using plotting `dmd_mod` with the new variables.
```{r dmd_modified_dplyr_ggplot}
dmd_mod <- //#A
  dmd %>%
  mutate(cpc = price / carats) %>%
  mutate(price_class = ifelse(cpc >= 3460, "Above Median", "Below Median")) %>%
  mutate(quality = ifelse(
    cut == "The Best" & clarity == "The Best", "Top Drawer", "The Rest")
  )
 
ggplot(dmd_mod, aes(x = carats, y = price)) + //#B
  geom_point() +
  facet_wrap(
    facets = vars(price_class, quality),
    labeller = label_both
  ) +
  labs(x = "Carats", y = "Price")
```

Moving between data transformation activities and plotting with ggplot like this is often valuable for better expressing the data you have to an audience, or, for exploring the data and getting insightful views that were otherwise hidden. Getting to a stage where you can rapidly translate your analysis/visualization needs to raw dplyr & ggplot code is worth the effort and understandably takes some practice.

Beyond Spreadsheets with R

This is an excerpt from Manning's book Beyond Spreadsheets with R.

Sometimes this is because you’ve mis-entered the name of the package you want (for example, ggplot instead of ggplot2). Other times it’s because the package explicitly requires a newer version of R than you have. This is partly due to CRAN requiring that new packages be built using the latest version of R, and CRAN enforces the same requirement on the users. In any case, it’s a good idea to remain up-to-date with the latest stable version of R, which may resolve this issue.

This is the default for ggplot(). Let’s add some data (see figure 9.2).

Figure 9.8 Aesthetics can be inherited by geometries if they’re provided in the ggplot() call.

ggplot_inherit-1_color.png

Because ggplot() follows the pen-and-paper model of plotting, the order in which you request the geom_s determines the order in which they are drawn. If you have features you want to appear above or below other features, then change the order in which they are added appropriately.