Note: nplots is an R package meant to create various ggplots for you! This package is heavily dependant on ggplot2 as the background machinery to render ggplots of your choice. It saves you the time of writing multiple lines of code by allowing you specify the kind of plot you want through issuing a few plot arguments.
This vignette summarises ways in which nplots can be used and the various arguments it can take.
To use nplots, you will require the following complementary packages:
#load packages library(nplots) library(tidyverse) library(gapminder) library(plotly) library(ggplot2)
If you don't have the above packages installed, please use the code below to have them installed:
#install the following required packages #install.packages("gapminder") #install.packages("ggplot2") #install.packages("plotly") #install.packages("tidyverse")
Please run the following lines in your R console for more details on the nplots package, the nplots_scatter and nplots_violin functions respectively.
?nplots
?nplots_scatter
?nplots_violin
nplots_scatter <- function(x, x_variable, y_variable, xlab="", ylab="", title="", plotly=TRUE, alpha=0.2, hjust=0.5){ if (!requireNamespace("ggplot2", "plotly", "gapminder", "tidyverse", quietly = FALSE)) { stop("Package \"pkg\" needed for this function to work. Please install it.", call. = FALSE)} base <- x %>% ggplot(aes(x_variable, y_variable)) + scale_y_log10() + labs(x = xlab, y = ylab) + theme_bw() + ggtitle(title) + theme(plot.title = element_text(hjust = hjust)) if (plotly == "TRUE"){ scatter_plot <- base + geom_point(alpha = alpha) + geom_smooth(method = lm) scatter_plot2 <- ggplotly(scatter_plot) return(scatter_plot2) } else if(plotly == "FALSE"){ scatter_plot <- base + geom_point(alpha = alpha) + geom_smooth(method = lm) return(scatter_plot) } else print ("sorry, I can't provide the plot you specified - please check the plot arguments supplied.") }
The example usage of the function nplots_scatter() function is:
nplots_scatter(gapminder, gapminder$lifeExp, gapminder$gdpPercap, xlab="lifeExp", ylab="gdpPercap", title="lifeExp Vs GdpPercap", plotly=FALSE)
ggplot takes on 2 main reguired arguments: An object of class ggplot() or a theme(). However, we can add more object types. To start:
Function argument x is the dataset to be used
The aesthetic mapping is supplied with aes() and describes how variables in the data are mapped to visual properties (aesthetics) of geoms. variable_x and variable_y provide the list of name value pairs mapped to variables in your dataset.
Given that this is a scatter plot, the layer to be added to this plot is geom_point() by default.
scales scale_x_continuous() and scale_y_continuous() are the default scales for continuous x and y aesthetics.
labs function argument to define the x and y lables that you would like to use in your plot
theme as an example, this version takes default theme_bw(), later versions will allow for flexibility in theme of choice
ggtitle function argument to define the title of your plot
plotly for users interested in rendering plots using plotly, specifying "FALSE" implies that your plot should not be rendered as a plotly plot otherwise set it to "TRUE"
alpha the alpha argument allows you to determine the opacity or transparency scales that should be used in a plot. Setting this value to 0.1 will render a more transparent layer compared to alpha=0.5. The ranges of these arguments should be between 0.1 and 1.
hjust the value captured by hjust determines the horizontal justification of the plot title. O.5 will center your title.
nplots_violin <- function(x, y_variable, factor, jitter=TRUE, order=TRUE, xlab="", ylab="", title="", plotly=TRUE, alpha=0.05, hjust=0.5){ if (!requireNamespace("ggplot2", "plotly", "gapminder", "tidyverse", quietly = FALSE)) { stop("Package \"pkg\" needed for this function to work. Please install it.", call. = FALSE)} if(plotly == TRUE){ violin_plot <- x %>% ggplot(aes(reorder(factor, y_variable),y_variable)) + geom_violin() + theme_bw() + geom_jitter(alpha = alpha) + ggtitle(title) + theme(plot.title = element_text(hjust = hjust)) + labs(x = xlab, y = ylab) violin_plot2 <- plotly::ggplotly(violin_plot) return(violin_plot2) } else if (plotly == FALSE){ violin_plot <- x %>% ggplot(aes(reorder(factor, y_variable),y_variable)) + geom_violin() + theme_bw() + geom_jitter(alpha=alpha) + ggtitle(title) + theme(plot.title = element_text(hjust = hjust)) + labs(x = xlab, y = ylab) return(violin_plot) } else print ("sorry, I can't provide the plot you specified - please check the plot arguments supplied") }
nplots_violin(gapminder, gapminder$gdpPercap, gapminder$continent, jitter=TRUE, order=TRUE, xlab="", ylab="gdpPercap", title="gdpPercap across contients", plotly=FALSE, alpha=0.05, hjust=0.6)
The arguments specified above apply here in with changes made to the call of the usage of some factor variables in a dataset.
Try loading a dataset other than gapminder and generate scatter and violin plots based on some variables in the dataset. The Iris dataset is good example to work with.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.