In this vignette, I will show you how to use these functions.
1. ggLogReg() make a scatterplot for logistic regression model
2. ggCLE() make a cleveland dot plot
3. ggDot() make a Wilkinson dot plot
Package moonBook is available on CRAN and github. Package moonBook2 is available only on github. Please install moonBook2 package using the following R code.
install.packages("devtools")
devtools::install_github("cardiomoon/moonBook")
devtools::install_github("cardiomoon/moonBook2")Because functions in “moonBook2” make interactive plots using package “ggplot2” and “ggiraph”, I strongly recommend you to install the latest version of the package “ggplot2” and “ggiraph” from github using following R command.
devtools::install_github("hadley/ggplot2")
devtools::install_github("davidgohel/ggiraph")ggLogReg() function draws a scatterplot for logistic regression model
require(ggplot2)
require(ggiraph)
require(moonBook2)
require(MASS) # for use of data biopsy
biopsy$malig=ifelse(biopsy$class=='malignant',1,0)
ggLogReg(biopsy,yvar="malig",xvar="V1",interactive=TRUE)You can zoom-in and zoom-out the plots with your mouse wheel.
The other example comes from data ‘radial’ included in package moonBook.
require(moonBook)
ggLogReg(radial,yvar="male",xvar="height",interactive=TRUE)ggCLE() draws a Cleveland dot plot. You can make a cleveland dot plot easily with the famous data mtcars. By default, the y axis variables are reordered by x-axis variables.
mtcars$name=rownames(mtcars)
ggCLE(mtcars,xvar="mpg",yvar="name",interactive=TRUE)If you want to get a plot in reversing order, set the parameter decreasing TRUE. You can set the colorvar parameter to set the color of segment and dot.
ggCLE(mtcars,xvar="mpg",yvar="name",colorvar="name",interactive=TRUE,decreasing=FALSE)You can make faceted plot by setting the parameter facetvar. If you want to draw top 20 mpg cars, set the parameter no=20.
ggCLE(mtcars,"mpg","name","am","am",no=20,interactive=TRUE)Next example is tophitters2001 data included in the package gcookbook.
require(gcookbook)Loading required package: gcookbook
str(tophitters2001)'data.frame': 144 obs. of 26 variables:
$ id : Factor w/ 144 levels "abreubo01","alfoned01",..: 138 128 41 3 59 4 13 17 24 69 ...
$ first: chr "Larry" "Ichiro" "Jason" "Roberto" ...
$ last : chr "Walker" "Suzuki" "Giambi" "Alomar" ...
$ name : chr "Larry Walker" "Ichiro Suzuki" "Jason Giambi" "Roberto Alomar" ...
$ year : int 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001 ...
$ stint: int 1 1 1 1 1 1 1 1 1 1 ...
$ team : Factor w/ 30 levels "ANA","ARI","ATL",..: 10 25 21 9 10 13 13 25 29 3 ...
$ lg : Factor w/ 2 levels "AL","NL": 2 1 1 1 2 2 2 1 1 2 ...
$ g : int 142 157 154 157 159 136 156 158 133 159 ...
$ ab : int 497 692 520 575 587 513 577 623 463 572 ...
$ r : int 107 127 109 113 132 79 110 118 77 113 ...
$ h : int 174 242 178 193 197 170 191 206 153 189 ...
$ 2b : int 35 34 47 34 54 31 55 37 31 33 ...
$ 3b : int 3 8 2 12 2 1 5 3 5 5 ...
$ hr : int 38 8 38 20 49 27 34 37 11 38 ...
$ rbi : int 123 69 120 100 146 108 126 141 54 102 ...
$ sb : int 14 56 2 30 7 5 7 5 15 9 ...
$ cs : int 5 14 0 6 5 1 9 5 5 10 ...
$ bb : int 82 30 129 80 98 57 92 40 39 98 ...
$ so : int 103 53 83 71 104 57 121 110 55 82 ...
$ ibb : int 6 10 24 5 15 14 5 5 3 20 ...
$ hbp : int 14 8 13 4 5 3 13 9 8 2 ...
$ sh : int 0 4 0 9 1 0 0 5 1 0 ...
$ sf : int 8 4 9 9 5 8 6 13 1 5 ...
$ gidp : int 9 3 17 9 14 18 8 11 5 13 ...
$ avg : num 0.35 0.35 0.342 0.336 0.336 ...
There are records of top 144 hitters in the tophitters2001 data. You can to draw a cleveland dot plot summarizing top 25 hitters(by average) by the following code.
ggCLE(tophitters2001,xvar="avg",yvar="name",no=25,interactive=TRUE)runs batted in hitters faceted by the league by the following code.
ggCLE(tophitters2001,xvar="rbi",yvar="name",colorvar="lg",facetvar="lg",no=25,interactive=TRUE)You can get a static ggplot by set the parameter interactive FALSE(default value).
ggCLE(tophitters2001,xvar="so",yvar="name",colorvar="lg",facetvar="lg",no=25,decreasing=FALSE)ggDot() make a Wilkinson dot plot. Like a histogram, you can make a Wilkinson dotplot by selecting x axis variable only.
require(moonBook) # for data radial
require(gcookbook) # for data heightweight
ggDot(heightweight,xvar="heightIn")You can assign a variable to fillvar to fill the dots.
ggDot(heightweight,xvar="heightIn",fillvar="sex")You can make dot plots with boxplots side-by-side by assign a categorial variable as to xvar and a continuous variable to yvar.
ggDot(heightweight,yvar="heightIn",xvar="sex",fillvar="sex")You can change the size of dot by setting the parameter binwidth. You can change the direction of stack by setting the stackdir parameter. You can adjust the positions of boxplot and dotplot with parameter position.
ggDot(radial,yvar="height",xvar="sex",fillvar="sex",stackdir="down",binwidth=1,position=0.1)If you wanted to draw a overapped dotplot overaaped with boxplot, set the position 0.
ggDot(radial,yvar="height",xvar="sex",fillvar="sex",boxfill="white",position=0,binwidth=1,boxwidth=0.5)