Through this post, I would like to share an update to my
RTutoR package. The first version of this package included an R Basics Tutorial App which I published earlier at DataScience+
The updated version of this package, which is now on CRAN, includes a plotting app. This app provides an automated interface for generating plots using the
ggplot2 package. Current version of the app supports 10 different plot types along with options to manipulate specific aesthetics and controls related to each plot type. The app also displays the underlying code which generates the plot and this feature would hopefully be useful for people trying to learn ggplot2. The app also utilizes the
plotly package for generating interactive plots which is especially suited for performing exploratory analysis.
A video demo on how to use the app is provided at the end of the post. The app is also hosted Shinyapps.io. However, unlike the package version, you would not be able to use your own dataset. Instead, the app provides a small set of pre-defined datasets to choose from.
High level overview of ggplot2
For people who are completely new to gglot2, or have just started working on it, I provide below a quick, concise overview of the ggplot2 package. This is not meant to be comprehensive, but just covers some key aspects so that it’s easier to understand how the app is structured and to make the most of it. You also can read a published tutorial in DataScience+ for ggplot2.
The template for generating a basic plot using ggplot2 is as follows:
ggplot(data_set) + plot_type(aes(x_variable,y_variable)) #For univariate analysis, you can specify just one variable
plot_type specifies the type of plot that should be constructed. There are more than 35 different plot types in ggplot2. (The current version of the app, however, supports only 10 different plot types)
ggplot2 provides an extensive set of options to manipulate different aesthetics of a plot. Aesthetics can be manipulated in one of two ways:
- Manually setting the aesthetic
- Mapping the aesthetic to a variable
To manually set the aesthetic, include the code outside the aes call. For example, the code below generates a scatter plot and colors the point red, using the color aesthetic
ggplot(iris) + geom_point(aes(Sepal.Length,Sepal.Width), color = "red")
To map the aesthetic to a variable, include the code inside the aes call. For example to color the scatter plot as per the Species type we will the modify the code above as follows:
ggplot(iris) + geom_point(aes(Sepal.Length,Sepal.Width,color = Species))
Not all aesthetics are applicable to all plot types. For e.g. the linetype aesthetic (which controls the line format), is not applicable to geom_point for instance (The app only displays those aesthetics which are applicable to the selected plot type)
A plot may also include controls specific to that plot type. Smoothing
curve(geom_smooth), for example, provides a “method” argument to control the smoothing function that is used for fitting the curve. ggplot2 provides an extensive set of options for different plot types (A good reference to read about the various options is ggplot2’s documentation here) The app does not include all the various options that are available, but tries to incorporate few of the most commonly used ones.
Multiple layers can be added to a plot. For example, the code below plots a scatter plot and fits a smoothing line as well:
ggplot(mtcars,aes(mpg,hp)) + geom_point() + geom_smooth()
Note: The way the app is coded, we need to specify the aesthetics for each plot separately, even if the aesthetics are same for each plot. Hence, if we construct this plot using the app, the underlying code that is displayed,would read:
ggplot(mtcars) + geom_point(aes(mpg,hp)) + geom_smooth(aes(mpg,hp))
- Performing SQL selects on R data frames
- Developing an R Tutorial shiny dashboard app
- Strategies to Speedup R Code
- Sentiment analysis with machine learning in R
- Sentiment Analysis on Donald Trump using R and Tableau