您的位置:首页 > 运维架构

Shopping cart analysis with R(-1) – Multi-layer pie chart

2015-01-30 09:07 302 查看
In this post, we will review a very interesting type of visualization – the multi-layer pie chart – and use it for one marketing analytics task – the shopping cart analysis. We will go from the initial data preprocessing to the
shopping cart analysis visualization. I will share the R code in that you shouldn’t write R code for every layer of chart. You can also find an example about how to create a multi-layer pie chart

here.
So, suppose we have a list of 1st orders/carts that were bought by our clients. Each order consists one or several products/category of products. Also, we want to visualise a relationship between products and see the share of orders
that includes each product or combination of products. The multi-layer pie chart can help us draw each product and its intersections with others.
After we loaded the necessary libraries with the following code:

we will simulate an example of the data set. Suppose we sell 4 products (or product categories):
a, b, c and d and each product can be sold with a different probability. Also, a client can purchase any combinations of products, e.g. only “a” or “a,b,a,d” and so on. Let’s do this with the
following code:

After this, we remove the duplicates. For example, if the order consists of more than one similar product (“a,b,a,d”), we want to exclude the effect of quantity.

Let’s see on our data set:

##   orderId product
## 1       1       a
## 2       1       b
## 3       2       a
## 4       2       c
## 5       3       a
## 6       4       a

In case you use real data, you should start with the following code (don’t forget to load libraries):

We have just created a product matrix. Note that, if you have more or fewer than 4 products, you should specify all of them instead of
a, b, c and d in the mutate() function.
The head of product matrix we obtain is:

##   orderId a   b    c    d    cart
## 1       1 a   b   <NA> <NA>  ab
## 2       2 a  <NA>  c   <NA>  ac
## 3       3 a  <NA> <NA> <NA>  a
## 4       4 a   b   <NA> <NA>  ab
## 5       5 a   b   <NA>  d    abd
## 6       6 a   b    c   <NA>  abc

After this, we need to calculate the total number of combinations:

## Source: local data frame [6 x 2]
##
##   cart num
## 1 a    123
## 2 ab   241
## 3 abc  168
## 4 abcd  71
## 5 abd  125
## 6 ac   105

From this point we start working on our multi-layer pie chart. My idea is to place one-product order inside the chart. Therefore, we are calculating the total number of products in every combination. Note: I will do this by using
the simple nchar() function, because we used a,
b
, c, and d as product names. In case you use real names, you can combine the names with a symbol. For example, “a-b-c-d” and count the number of “-” symbols.

Because I want a chart to start from the maximum combination of products, I arranged the sev.prod data set with the last function. Therefore, the data is ready for plotting. We are defining parameters for the chart:

Note: if you have more than 4 products, you should add extra colors into the palette
all.colors. Finally, our chart:





In case you want to add some statistics on plot, e.g. total number of each combination or share of combinations in total amount, we just need to create this table and add it on plot with the following code:





Therefore, we visualized the relationship between the products in the orders. However, this is just an example of using multi-layer pie chart. Feel free to share your ideas or comments!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: