Introduction of R package moonBook2(3)

Keon-Woong Moon

2016-06-11

Package installation

Package moonBook is avaiable 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")

In this vignette, I will show you how to visualize the interaction between two predictor variables.

  1. PopPyramid(): get population data from internet and make a population pyramid.
  2. ggPoints(): make a scatterplot with/without regression line
  3. ggBar(): make a interactive barplot
  4. ggRose(): make a interactive Rose plot
  5. ggHeatmap(): make a interactive Rose plot

PopPyramid() for population pyramid

You can’t make a population pyramid without data; PopPyramid() function gets population data data from US Census Bereau. This function uses get_popdata() function internally and this function is a modification of get_data() function of Kyle Walker’s. You can use PopPyramid function with the following format.

PopPyramid(country,year,interactive=FALSE)

The country parameter shoud be one of the FIPS 10-4 country code. If you wnated to make a population pyramid 2016 for Nigeria, you can use the following code.

require(moonBook2)
require(ggplot2)
require(ggiraph)
require(moonBook)

PopPyramid("NI",2016,interactive=TRUE)

The followings are other examples.

PopPyramid("KS",2016,interactive=TRUE)  # for South Korea
PopPyramid("US",2016,interactive=TRUE)  # for United States

ggPoints() for scatterplot

You can make interactive scatter plot by ggPoints(). You can make a scatterplot by two different ways.

by using column name

require(plotly)
ggPoints(iris,Sepal.Width,Sepal.Length,interactive=TRUE)

You can assign a grouping variable to the colour or the fill parameter.

ggPoints(iris,Sepal.Width,Sepal.Length,colour="Species",interactive=TRUE )

You can remove the regression line by setting the smooth parameter ‘FALSE’.

ggPoints(iris,Sepal.Width,Sepal.Length,colour="Species",smooth=FALSE,interactive=TRUE )

You can adjust the method of regression by setting the method from “auto” to other method and adjust the formula option with formula parameter.

ggPoints(mtcars,mpg,wt,colour="am",method="lm",formula="y~poly(x,2)",interactive=TRUE)

by formula

You can use formula as follows.

ggPoints(mpg~wt,data=mtcars,interactive=TRUE )
ggPoints(mpg~wt|am,data=mtcars,interactive=TRUE )

For radial artery atherosclerosis data, you can draw scatter plot as follows.

require(car)
ggPoints(salary~yrs.service|sex,data=Salaries,method="lm",formula="y~poly(x,2)",interactive=TRUE )

If you wanted to add another layers or options, you can make a ggplot without interactive=TRUE(the default value of interactive is FALSE) and add option(s) to the ggplot.

require(car)

p<-ggPoints(salary~yrs.service|sex,data=Salaries,method="lm",formula="y~poly(x,2)")
p<-p+facet_wrap(~sex)
p

ggplotly(p)

ggBar() for easy interactive barplot

You can make interactive barplot by ggBar().

ggBar(acs,"Dx","smoking",interactive=TRUE)

You can add labels to barplot easily by setting the parameter addlabel=TRUE

ggBar(acs,"Dx","smoking",addlabel=TRUE,interactive=TRUE)
ggBar(acs,"Dx","smoking",position="dodge",addlabel=TRUE,interactive=TRUE)
ggBar(acs,"Dx","smoking",position="fill",addlabel=TRUE,interactive=TRUE)

You can make horizontal bar plot easily by setting the parameter horizontal=TRUE

ggBar(acs,"Dx","smoking",position="fill",addlabel=TRUE,horizontal=TRUE,yangle=90,
      width=0.5,interactive=TRUE)

You can make polar map by setting the parameter polar=TRUE

ggBar(acs,"Dx","smoking",interactive=TRUE,width=1,colour="white",size=0.2,polar=TRUE)

You can make barplot from preprocessed data.

ggBar(rose,Month,group,"value",stat="identity",interactive=TRUE)
## Using nrow as value column: use value.var to override.

ggRose() ; for interactive Rose plot

You can make rose plot by using ggBar().

ggBar(rose,Month,group,"value",stat="identity",polar=TRUE,palette="Reds",width=1,
       color="black",size=0.1,interactive=TRUE)
## Using nrow as value column: use value.var to override.

You can make rose plot more easily by using ggRose()

ggRose(rose,Month,group,"value",interactive=TRUE)
## Using nrow as value column: use value.var to override.

Here comes another example of ggRose().

ggRose(acs,"Dx","smoking",interactive=TRUE)

ggHeatmap() ; for easy interactive heatmap

In heatmap, you can assign a continuous variable to the fill color. You can make interactive heatmap by using ggHeatmap()

ggHeatmap(acs,"Dx","smoking",interactive=TRUE)

You can add labels to heatmap easily by setting the parameter addlabel=TRUE

ggHeatmap(acs,"Dx","smoking",addlabel=TRUE,interactive=TRUE)

You can make heatmap with preprocessed data.

ggHeatmap(rose,"group","Month","value",stat="identity",interactive=TRUE)

You can change the fill colors by seting the parameter gradient_colors()

ggHeatmap(rose,"group","Month","value",stat="identity",gradient_colors = c("white","red"),interactive=TRUE)

You can make polar heatmap by seting the parameter polar TRUE.

ggHeatmap(rose,"Month","group","value",stat="identity",polar=TRUE,interactive=TRUE)

Another example comse from taco data. This data is made by “Aaron Richter”. Please take a look at one of his wonderful post Communicating Experimental Results with R.

head(taco)
  ShellType Filling AgeGroup Rating
1      Hard Chicken      <13  0.964
2      Hard Chicken    13-20  0.906
3      Hard Chicken    21-39  0.819
4      Hard Chicken      40+  0.599
5      Hard    Beef      <13  0.983
6      Hard    Beef    13-20  0.918
str(taco)
'data.frame':   136 obs. of  4 variables:
 $ ShellType: chr  "Hard" "Hard" "Hard" "Hard" ...
 $ Filling  : chr  "Chicken" "Chicken" "Chicken" "Chicken" ...
 $ AgeGroup : chr  "<13" "13-20" "21-39" "40+" ...
 $ Rating   : num  0.964 0.906 0.819 0.599 0.983 0.918 0.866 0.629 0.985 0.915 ...

This data consisits of ratings of taco by shell types, age groups and filllings. You can make heatmap with this data.

ggHeatmap(taco,"AgeGroup","Filling","Rating",stat="identity",interactive=TRUE)

You add facets to you heatmap.

ggHeatmap(taco,"AgeGroup","Filling","Rating",facetvar="ShellType",
          stat="identity",interactive=TRUE)