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 make a choropleth map with moonBook2 package.
A choropleth map is a map with regions that are colored according to variable values. To make a choropleth map, two data is required. One is the map data. The other is data for colors. For example, you can get the map data of Unites States by using the following code.
require(ggplot2)
require(moonBook2)
require(ggiraph)
states_map <- map_data("state")You can make a US crime data by following code.
crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
crimes state Murder Assault UrbanPop Rape
Alabama alabama 13.2 236 58 21.2
Alaska alaska 10.0 263 48 44.5
Arizona arizona 8.1 294 80 31.0
Arkansas arkansas 8.8 190 50 19.5
California california 9.0 276 91 40.6
Colorado colorado 7.9 204 78 38.7
Connecticut connecticut 3.3 110 77 11.1
Delaware delaware 5.9 238 72 15.8
Florida florida 15.4 335 80 31.9
Georgia georgia 17.4 211 60 25.8
Hawaii hawaii 5.3 46 83 20.2
Idaho idaho 2.6 120 54 14.2
Illinois illinois 10.4 249 83 24.0
Indiana indiana 7.2 113 65 21.0
Iowa iowa 2.2 56 57 11.3
Kansas kansas 6.0 115 66 18.0
Kentucky kentucky 9.7 109 52 16.3
Louisiana louisiana 15.4 249 66 22.2
Maine maine 2.1 83 51 7.8
Maryland maryland 11.3 300 67 27.8
Massachusetts massachusetts 4.4 149 85 16.3
Michigan michigan 12.1 255 74 35.1
Minnesota minnesota 2.7 72 66 14.9
Mississippi mississippi 16.1 259 44 17.1
Missouri missouri 9.0 178 70 28.2
Montana montana 6.0 109 53 16.4
Nebraska nebraska 4.3 102 62 16.5
Nevada nevada 12.2 252 81 46.0
New Hampshire new hampshire 2.1 57 56 9.5
New Jersey new jersey 7.4 159 89 18.8
New Mexico new mexico 11.4 285 70 32.1
New York new york 11.1 254 86 26.1
North Carolina north carolina 13.0 337 45 16.1
North Dakota north dakota 0.8 45 44 7.3
Ohio ohio 7.3 120 75 21.4
Oklahoma oklahoma 6.6 151 68 20.0
Oregon oregon 4.9 159 67 29.3
Pennsylvania pennsylvania 6.3 106 72 14.9
Rhode Island rhode island 3.4 174 87 8.3
South Carolina south carolina 14.4 279 48 22.5
South Dakota south dakota 3.8 86 45 12.8
Tennessee tennessee 13.2 188 59 26.9
Texas texas 12.7 201 80 25.5
Utah utah 3.2 120 80 22.9
Vermont vermont 2.2 48 32 11.2
Virginia virginia 8.5 156 63 20.7
Washington washington 4.0 145 73 26.2
West Virginia west virginia 5.7 81 39 9.3
Wisconsin wisconsin 2.6 53 66 10.8
Wyoming wyoming 6.8 161 60 15.6
Because the states are coded in lower cases in states_map, you have to use lower case in you data. You can make a choropleth map showing the murder rate easily by the following code.
ggChoropleth(crimes,states_map,fill="Murder",map_id="state",interactive=TRUE)You can make a choropleth map with different colors.
ggChoropleth(crimes,states_map,fill="UrbanPop",map_id="state",
colors=c('white','steelblue'),interactive=TRUE)If you want to make a faceted choroplath map, you have to reshape your data into long form. For example, the crimes data can be reshaped to a long form by the following R code.
crimeslong=reshape2::melt(crimes,id="state")
str(crimeslong)'data.frame': 200 obs. of 3 variables:
$ state : Factor w/ 50 levels "alabama","alaska",..: 1 2 3 4 5 6 7 8 9 10 ...
$ variable: Factor w/ 4 levels "Murder","Assault",..: 1 1 1 1 1 1 1 1 1 1 ...
$ value : num 13.2 10 8.1 8.8 9 7.9 3.3 5.9 15.4 17.4 ...
And then you can make static ggplot with ggChoropleth() function first and then add the facets by facet_wrap function nad finally call ggiraph() function to draw a interactive map.
p<-ggChoropleth(crimeslong,states_map,fill="value",map_id="state",facetvar="variable",
colors=c('white','green','steelblue','red'),interactive=TRUE)
pYou can make barplot with errobars easliy with ggErrorBar(). With Salaries data, you can summarize the salary of professors with mean and se/sd and draw a bar plot with errorbar.
require(car) # for use of data Salaries## Loading required package: car
ggErrorBar(Salaries,yvar="salary",xvar="rank",group="sex",interactive=TRUE)By default, the error bar is displayed two-sided. You can display errorbar one-side only by change the mode=1.
require(moonBook) # for use of data acs## Loading required package: moonBook
ggErrorBar(acs,"age","Dx","sex",interactive=TRUE,mode=1)ggErrorBar(acs,"age","Dx",interactive=TRUE,mode=1)You can use ggErrorBar() function with formula.
ggErrorBar(age~Dx*sex,data=acs,interactive=TRUE)