DataScience+ We share R tutorials from scientists at academic and scientific institutions with a goal to give everyone in the world access to a free knowledge. Our tutorials cover different topics including statistics, data manipulation and visualization!
Visualizing Data

Plotting App for ggplot2 – Part 2

Through this post, I would like to provide an update to my plotting app, which I first blogged about here.
The app is available as part of my package RtutoR, which is published on CRAN.(The app is also hosted at However, unlike the package version, you would not be able to use your own dataset with this hosted version)

The plotting app was developed to provide an automated interface for generating plots using the ggplot2 package. At the end of this post, is a video demo on how to use the app, with the demo specifically focusing on the new features and functionalities that have been added to this version of the app.

I also provide below a brief overview of these new functionalities:


Faceting is a very useful feature in ggplot which allows us to split our data by one or more variables, and generate a plot on each subset of the data.
For e.g, the code below will generate a scatter plot depicting the relationship between the price of the diamond and it’s carat for each cut of the diamond

 ggplot(diamonds) + geom_point(aes(x=price,y=carat)) + facet_wrap(~cut) 

Note that facet_grid does something very similar; It primarily differs in the way the resulting plots are laid out and arranged on the grid (with a few other subtle differences, for e.g. refer to the Stack Overflow discussion here)

Currently, the plotting app only supports facet_wrap with one variable.

Color palette

When a color (or fill) aesthetic is mapped to a variable, ggplot2 uses a default coloring scheme to color the different levels of the variable (in the case of categorical variables) or uses a color gradient scheme to represent the range of values (in case of continuous variables). ggplot2 provides various other color schemes to choose from, if you are not happy with the default coloring scheme.

For discrete variables, an excellent choice is the brewer scale (which is taken from the RColorBrewer package), which is what the plotting app supports. There are more than 30 different palettes to choose from, divided into 3 color schemes – Sequential, Qualitative and Diverging.

For continuous variables, scale_color_gradient provides an easy and convenient way to alter the color scheme. For e.g, the code below plots the relationship between price and carat, however, instead of using the default coloring scheme for price, the price is now colored from yellow (for low values) to red (high values)

 ggplot(diamonds) + geom_point(aes(price,carat,col=price)) + scale_color_gradient(low="yellow",high="red") 

Axis range control

Allowing the users to manually alter the axis range is another enhancement in this version of the plotting app. This feature can be used, for instance, to remove outliers from your plot or to help you zoom in on a specific section of the plot.

For e.g. let’s generate a box plot depicting the price distribution for each cut of the diamond:

 ggplot(diamonds) + geom_box(aes(x=cut,y=price)) 

If you are really interested in just analyzing the Inter Quartile distribution for different cuts, you can limit the length of the tail as follows:

 ggplot(diamonds) + geom_boxplot(aes(x=cut,y=price)) + ylim(c(0,10000)) 

By limiting the length of the tails, the plot zooms out providing a better comparison of the price distribution for different cuts.

(The plotting app provides a slider input for manually adjusting the axis ranges – The slider ranges are dynamic and is based on the minimum and maximum values for the selected variable)

Update: As one of the readers (Heather Turner) has pointed out in her comment below, zooming the plot using scales results in points outside the range to be converted to NA (ggplot2 gives a warning indicating that these NAs have been removed from the plot). If you would like to retain all the data points you can use coord_cartesian instead (though the current version of the app does not support it). You can read more about it here.

Color Picker

Previous version of the app provided a color drop-down for selecting colors, for manually setting the color/fill aesthetic. The dropdown had more than 600 different colors to choose from (generated using the colors() function in R)
However, the new version of the app, replaces the color dropdown with a color picker (available as part of the shinyjs package). Color picker provides a far more more intuitive and convenient way to choose a color of your choice (The underlying code which is displayed on the app, will also provide the hex code for the selected color).

Hope you find this useful and check my github account.