Customizing your charts

As mentioned before, one of the big advantages of ggplot2 is that you can change nearly all elements according your individual needs. In the following section, we will show you some ways to customize your visualizations.

Subsetting your data

A very handy feature of ggplot2 is the way you can subset data. Just add a subset function call to your graph to just visualize certain values. The subset you want to visualize can be set for all geoms:

ggplot(iris) %+% subset(iris,Species == "setosa") + geom_point(aes(Sepal.Length, Sepal.Width))

Otherwise, you can set it for one geom in your graph:

ggplot(iris,aes(x = Sepal.Length,y = Sepal.Width,color = Species))  + geom_point(data = subset(iris, Species %in% c("setosa","virginica")))

Get Mastering RStudio – Develop, Communicate, and Collaborate with R now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.