Plotting App for ggplot2 (Part 2)

[This article was first published on DataScience+, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 shinyapps.io. 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

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)

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.

Related Post

  1. Mastering R plot – Part 3: Outer margins
  2. Interactive plotting with rbokeh
  3. Mastering R plot – Part 2: Axis
  4. How to create a Twitter Sentiment Analysis using R and Shiny
  5. Visualizing MLS Player Salaries with ggplot2

To leave a comment for the author, please follow the link and comment on their blog: DataScience+.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)