O'Reilly logo

Mastering RStudio – Develop, Communicate, and Collaborate with R by Maximilian H. Nierhoff, Julian Hillebrand

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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")))

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required