hpackedbubble: Create Split Packed Bubble Charrts

hpackedbubble: Create Split Packed Bubble Charrts

This is my second R package submitted to CRAN, By binding R functions and ‘Highcharts’ library, ‘hpackedbubble’ provides a simple way to draw split packed bubble charts. I have already show the examples of this package at my last tweet. Just like these:

When using this package, please pay attention to whether your computer is connected to the Internet. The software package can only work properly when there is a network connection.

Installation

You can install it from CRAN or my GitHub:

1
2
3
4
# From CRAN:
install.packages("hpackedbubble", build_vignettes = TRUE)
# From GitHub:
devtools::install_github("czxa/hpackedbubble", build_vignettes = TRUE)

Vignettes

I prepared a vignette for you, it can be found by runing vignette("hpackedbubble") at your R consol. Also, you can read it at CRAN: Create Split Packed Bubble Charts or Rpus: Create Split Packed Bubble Charts. I also write a Chinese version vignette for Chinese users: 使用 hpackedbubble 包绘制分割圆锥气泡图.

Load hpackedbubble package

1
library(hpackedbubble)

I preset a lot parameters for ‘hpackedbubble()’ function. Of these parameters, four are particularly important: cat, name, value and split. I have prepared a demo data set for the use of this package. You can load it by executing data(CO2) or CO2 = CO2(Remeber to load ‘hpackedbubble’ package first). Then you will find a dateset named CO2 appears at your Environment(Supposed you are using RStudio).

This data set contains data on carbon dioxide emissions worldwide in 2014. I sorted it out from ‘highcharts’. Note that ‘hpackedbubble’ package has a dependency on Highcharts, a commercial JavaScript charting library. Highcharts offers both a commercial license as well as a free non-commercial license. Please review the licensing options and terms before using this software, as the ‘hpackedbubble’ license neither provides nor implies a license for Highcharts. Highcharts (http://highcharts.com) is a Highsoft product which is not free for commercial and Governmental use.

Get a glimpse to CO2 data

First, we can use glimpse(CO2) to get a glimpse to this data:

1
2
3
4
5
6
7
library(dplyr)
glimpse(CO2)
#> Observations: 116
#> Variables: 3
#> $ continent <chr> "Europe", "Europe", "Europe", "Europe", "Europ…
#> $ country <chr> "Germany", "Croatia", "Belgium", "Czech Republ…
#> $ CO2 <dbl> 767.1, 20.7, 97.2, 111.7, 158.1, 241.6, 249.1,…

A Simple Example

This data set has 116 observations and three variables: continent, country, and CO2. Here, continent is categorized variable, corresponding to cat parameter; country is name variable, corresponding to name parameter and CO2 is value variable, corresponding to value parameter.

1
2
3
hpackedbubble(cat = CO2$continent,
name = CO2$country,
value = CO2$CO2)

This is an interactive chart, which means that you can use mouse to drag these bubbles and see the information corresponding to each bubbles.

The defalut value of split parameter is 1, which means to render a split packed bubble chart, just like the last picture. If you set split = 1, the result will be like this:

1
2
3
4
hpackedbubble(cat = CO2$continent,
name = CO2$country,
value = CO2$CO2,
split = 0)

This is a ordinary bubble diagram.

Themes

There have 9 kinds of theme you can use: darkgreen, darkblue, avocado, darkunica, gray, gridlight, grid, sandsignika and sunset. The default theme is ‘sandsignika’.

Using hpackedbubble within ‘RMarkdown’ Documents and ‘Shiny’ applications

Of course, it can be used in RMarkdown documents and Shiny applications. I prepare a demo shiny applications in these package, which conclude all parameters, you can run it by:

1
2
3
dir <- system.file("examples", "hpackedbubble", package = "hpackedbubble")
setwd(dir)
shiny::shinyAppDir(".")

A real world example

The reticulate provides a interface between R and Python, at following example, I use reticulate package load Python function in R.

First, write following Python codes in a blank file named getfriends.py(Remeber to install itchat by running pip install itchat in your terminal):

1
2
3
4
5
6
7
8
def getfriends():
import itchat
import pandas as pd
itchat.auto_login(hotReload = True)
friends = itchat.get_friends(update = True)
itchat.logout()
friends = pd.DataFrame(friends)
return(friends)

Next, source this file in R:

1
2
library(reticulate)
reticulate::source_python('getfriends.py')

Then, you will find a getfriends() function appears in Environment. Next:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
df <- getfriends()
df %<>%
group_by(Province, City) %>%
summarise(n = n()) %>%
ungroup() %>%
mutate(
Province = gsub(pattern = "[a-z, A-Z]", Province, replacement = "")
) %>%
filter(Province != "" & Province != "--") %>%
filter(City != "" & City != "--")
df
#> # A tibble: 77 x 3
#> Province City n
#> <chr> <chr> <int>
#> 1 安徽 安庆 1
#> 2 安徽 池州 2
#> 3 安徽 滁州 1
#> 4 安徽 阜阳 32
#> 5 安徽 合肥 2
#> 6 安徽 淮南 1
#> 7 安徽 马鞍山 1
#> 8 安徽 芜湖 3
#> 9 北京 朝阳 1
#> 10 北京 大兴 1
#> # … with 67 more rows

Then we can draw a split packed bubble chart:

1
2
3
4
5
6
7
8
hpackedbubble(df$Province, df$City, df$n, 
title = "My Wechat Friends",
subtitle = "czxa.top",
theme = "subset",
pointFormat = "<b>{point.name}:</b> {point.y} friends",
dataLabelsFilter = 2,
packedbubbleMinSize = "20%",
packedbubbleMaxSize = "150%")

Alse, bubble chart:

1
2
3
4
5
6
7
8
9
hpackedbubble(df$Province, df$City, df$n, 
title = "My Wechat Friends",
subtitle = "czxa.top",
theme = "subset",
pointFormat = "<b>{point.name}:</b> {point.y} friends",
dataLabelsFilter = 2,
packedbubbleMinSize = "50%",
packedbubbleMaxSize = "250%",
split = 0)

Beautiful and interesting!

‘hchinamap’ package has just landed on CRAN!

Just now I got good news that my ‘hchinamap’ package was also adopted by CRAN. By binding R functions and the ‘Highmaps’ https://www.highcharts.com.cn/products/highmaps chart library, ‘hchinamap’ package provides a simple way to map China and its provinces. The map of China drawn by this package contains complete Chinese territory, especially the Nine-dotted line, South Tibet, Hong Kong, Macao and Taiwan. I will introduce this package in next tweet.

We can use ‘hchinamap’ to map my wechat friends’s distribution:

1
2
3
4
5
6
7
8
9
10
11
12
# Installation
install.packages("hchinamap")

# Draw maps
library(hchinamap)
chinadf <- df %>%
group_by(Province) %>%
summarise(value = sum(n))
hchinamap(name = chinadf$Province, value = chinadf$value,
region = "China", title = "My Wechat Friends",
subtitle = "czxa.top",
theme = "sandsignika")

Friends in Guangdong:

1
2
3
4
5
6
7
8
guangdong <- df %>% 
filter(Province == "广东") %>%
group_by(City) %>%
summarise(value = sum(n))
hchinamap(name = guangdong$City, value = guangdong$value,
region = "Guangdong", title = "My Wechat Friends",
subtitle = "czxa.top",
theme = "sandsignika")

Funny!

Now, three R packages on CRAN

One of the important reasons I submitted R packages to CRAN was that I wanted them to be discovered and used by more people. Although the number of people used is not very large, but it is still a happy thing!

The cranlogs package can be used to monitor the changes of R package downloads on CRAN. Here are my three R packages’ download volume curve:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
library(cranlogs)
library(highcharter)
crandf <- cran_downloads(c("hwordcloud", "hpackedbubble", "hchinamap"),
from = "2019-08-07",
to = "last-day") %>% as_tibble()
hchart(crandf, "line",
hcaes(x = date, y = count, group = package),
color = RColorBrewer::brewer.pal(3, "Set2")) %>%
hc_title(text = "My R Packages on CRAN") %>%
hc_add_theme(hc_theme_chalk()) %>%
hc_xAxis(title = list(text = "日期"),
dateTimeLabelFormats = list(day = "%m-%d"),
labels = list(autoRotation = TRUE)) %>%
hc_yAxis(title = list(text = "每日下载量"))

unsplash-logoDaniil Silantev

# R

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×