Introduction to package ggiraphExtra

Keon-Woong Moon

2016-11-30

Package ggiraphExtra contains many useful functions for exploratoty plots. These functions are made by both ‘ggplot2’ and ‘ggiraph’ packages. You can make a static ggplot or an interactive ggplot by setting the parameter interactive=TRUE.

Package installation

You can install package ggiraphExtra with the following command.

#install.packages("devtools")
devtools::install_github("cardiomoon/ggiraphExtra")

ggPoints() for interactive scatterplot with regression equation

You can make interactive ggplot easily. You can hover the points, see the regression equations and zoom-in zoom-out with the mouse wheel.

require(ggplot2)
require(ggiraph)
require(ggiraphExtra)

ggPoints(aes(x=wt,y=mpg,color=am),data=mtcars,method="lm",interactive=TRUE)

You can draw scatter plot for binary dependent variable. The GBSG2 data contains data of 686 observations from the German Breast Cancer Study Group 2(GBSG2) study. You can get logistic regression line with a jittered scatterplot by setting the parameter method glm.

require(TH.data)
data(GBSG2)
ggPoints(aes(x=pnodes,y=cens),data=GBSG2,method="glm",interactive=TRUE)

Please check the vignette for “ggPoints” at http://rpubs.com/cardiomoon/231822

ggRadar() for interactive radar chart

You can explore a data.frame with ggRadar() or ggBoxplot().

ggRadar(data=iris,aes(color=Species),interactive=TRUE)
ggRadar(data=mtcars,aes(colour=am),interactive=TRUE)

ggBoxplot() ; interactive boxplot

ggBoxplot() draws boxplots for all continuous variables in the data.frame. You can make horizontal boxplots by setting the parameter horizontal=TRUE.

ggBoxplot(iris,aes(color=Species),interactive=TRUE)
ggBoxplot(mtcars,aes(x=c(mpg,cyl,disp,hp,drat),color=am),rescale=TRUE,horizontal=TRUE,interactive=TRUE)

ggSpine() for an interactive spinogram

ggSpine() is a interactive ggplot version of spineplot(). Spine plots are a special cases of mosaic plots, and can be seen as a generalization of stacked (or highlighted) bar plots. Analogously, spinograms are an extension of histograms. You can add labels by setting the parameter addlabel=TRUE.

require(moonBook)
ggSpine(data=acs,aes(x=age,fill=smoking),interactive=TRUE)
ggSpine(data=acs,aes(x=Dx,fill=smoking),addlabel=TRUE,interactive=TRUE)

## ggBar() for an interactive barplot

ggBar() draws interactive barplot. You can add labels, draw horizontal barplots or polar plots. You can draw histogram with ggBar()

ggBar(acs,aes(x=Dx,fill=smoking),addlabel=TRUE,horizontal=TRUE,width=0.5,interactive=TRUE)
ggBar(rose,aes(x=Month,fill=group,y=value),stat="identity",polar=TRUE,palette="Reds",width=1,
       color="black",size=0.1,interactive=TRUE)
ggBar(acs,aes(x=age,fill=smoking),addlabel=TRUE,horizontal=TRUE,width=0.5,interactive=TRUE)

ggRose() for an interactive rose plot

ggRose() is a shortcut of ggBar(…,polar=TRUE,palette=“Reds”,…).

ggRose(acs,aes(x=Dx,fill=smoking),interactive=TRUE)
ggRose(rose,aes(x=Month,fill=group,y=value),interactive=TRUE)

ggPair() for an interactive scatter plot with line plot

ggPair(iris[3:5],interactive=TRUE)
ggPair(iris,aes(color=Species),horizontal=TRUE,interactive=TRUE)

ggPieDonut() for a pie and donut plot

You can draw a pie and donut plot with ggPieDonut().

ggPieDonut(acs,aes(pies=Dx,donuts=smoking),interactive=TRUE)
ggPieDonut(browsers,aes(pies=browser,donuts=version,count=share),interactive=TRUE)
ggDonut(browsers,aes(donuts=version,count=share),interactive=TRUE)

## ggDot() for a Wilkinson dot plot

require(gcookbook) # for data heightweight

ggDot(heightweight,aes(sex,heightIn,fill=sex),boxfill="white",binwidth=0.4)

ggDot(radial,aes(x=height,fill=sex),binwidth=1)

ggCLE() for a cleveland dot plot

mtcars$name=rownames(mtcars)
ggCLE(data=mtcars,aes(x=mpg,y=name,color=am,facet=am),interactive=TRUE)
ggCLE(data=tophitters2001,aes(x=avg,y=name,color=lg,facet=lg),no=20,interactive=TRUE)

ggCor() for a correlation plot

require(mycor)
ggCor(iris,label=2,interactive=TRUE)
ggCor(mtcars,interactive=TRUE)

ggHeatmap() for an interactive heatmap

ggHeatmap(acs,aes(x=Dx,y=smoking),addlabel=TRUE,interactive=TRUE)
ggHeatmap(taco,aes(x=AgeGroup,y=Filling,fill=Rating,facet=ShellType),stat="identity",interactive=TRUE)

ggAncova() for an ANCOVA model

You can draw an ANCOVA(one-way anova with one covariate) model.

fit=lm(NTAV~age+HBP,data=radial)
ggAncova(fit,interactive=TRUE)
ggAncova(NTAV~age+DM,data=radial,interactive=TRUE)

ggEffect() for a linear regressin with interaction model

You can draw a linear regression with an interaction model

ggEffect(NTAV~age*smoking,data=radial,interactive=TRUE)
fit=lm(mpg~wt*hp,data=mtcars)
ggEffect(fit,interactive=TRUE)

ggHSD(): Draw Tukey Honest Significant Differences plot

fm1 <- aov(breaks ~ wool + tension, data = warpbreaks)
result=TukeyHSD(fm1, "tension", ordered = TRUE)
ggHSD(result,interactive=TRUE)

ggErrorBar() for a barplot with error bar

ggErrorBar(mpg,aes(x=drv,y=hwy,color=cyl),mode=1,interactive=TRUE,errorbar="sd")

ggCatepillar()

ggCatepillar(acs,aes(Dx,age,color=HBP),interactive=TRUE)
ggCatepillar(acs,aes(age,height,color=sex),errorbar=FALSE,interactive=TRUE)

ggBidirectionalBar()

PopPyramid("JA",2015,interactive=TRUE)
KS2016=get_popdata("KS",2016)
ggBidirectionalBar(data=KS2016,aes(x=c(Male,Female),y=Age),addlabel=TRUE,margin=1.3,interactive=TRUE)

ggChoropleth()

You can draw an interactive choropleth map easily. You can assign one or several variable(s) to ‘fill’ aesthetics. If you do not assign variable(s), all continous variables are assigned.

crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
states_map <- map_data("state")
ggChoropleth(crimes,aes(fill=Murder,map_id=state),map=states_map,interactive=TRUE)
ggChoropleth(crimes,aes(fill=c(Murder,Rape),map_id=state),map=states_map,interactive=TRUE)
ggChoropleth(crimes,aes(map_id=state),map=states_map,interactive=TRUE)