廿TT

譬如水怙牛過窓櫺 頭角四蹄都過了 因甚麼尾巴過不得

{googleAnalyticsR} {ggplot2} 内訳をドリルダウンしていく棒グラフ

ggplot2 で左から右に内訳をドリルダウンしていく棒グラフ(ツリーマップ?)を書きました。

f:id:abrahamcow:20170508093347p:plain

新規訪問で自然検索経由のデスクトップを利用している男性の25歳〜34歳のCVが多いことがわかります。

ついで再訪問でソーシャル経由訪問のモバイルを利用している男性の25歳〜34歳のCVが多いようです。

R のコードです。

library(googleAnalyticsR)
library(cowplot)
library(tidyr)
library(dplyr)
library(plyr)
ga_auth()
account_list <- ga_account_list()
ga_id <- account_list[3,'viewId']

CVfilter <- 
  filter_clause_ga4(list(met_filter("goal3Completions", 
                                    "GREATER_THAN", 
                                    "0")))
dims <-c("userType","channelGrouping","deviceCategory","userGender","userAgeBracket")
gadata <-
  google_analytics_4(ga_id,
                     date_range = c("2017-04-01","2017-04-30"),
                     metrics = c("goal3Completions"),
                     met_filters = CVfilter,
                     dimensions = dims,
                     max = 20000)

len <- length(dims)
out <- vector("list",len)

dat1 <-ddply(gadata, dims[1], summarize, CV = sum(goal3Completions)) 

dat1$level <-dat1[,1]
dat1 <- dat1 %>% 
  setNames(c("level","CV","label")) %>% 
  arrange(desc(level)) %>% 
  mutate(pos=cumsum(CV)-CV/2,dimensions=dims[1])

out[[1]] <-dat1
for(i in 2:len){
out[[i]] <-ddply(gadata, dims[1:i], summarize, CV = sum(goal3Completions)) %>% 
    unite(level,1:i,remove=FALSE) %>% 
    select(-c(2:i)) %>% 
    setNames(c("level","label","CV")) %>% 
    arrange(desc(level)) %>% 
    mutate(pos=cumsum(CV)-CV/2,dimensions=dims[i])
}

out_df <- bind_rows(out)

out_df <- mutate(out_df,dimensions=factor(dimensions,levels = dims))

ggplot(out_df,aes(x=dimensions,y=CV))+
  geom_col(aes(group=level,fill=label),colour="black",alpha=0.5,width=1)+
  geom_label(aes(y=pos,label=label))+
  theme(legend.position = "none")

データセット(gadata)はこんな形で与えられています。

"userType","channelGrouping","deviceCategory","userGender","userAgeBracket","goal3Completions"
"New Visitor","Organic Search","desktop","female","18-24",1
"New Visitor","Organic Search","desktop","female","55-64",1
"New Visitor","Organic Search","desktop","male","18-24",1
"New Visitor","Organic Search","desktop","male","25-34",5
"New Visitor","Organic Search","desktop","male","35-44",2
"New Visitor","Organic Search","desktop","male","45-54",1
"New Visitor","Organic Search","desktop","male","55-64",1
"New Visitor","Organic Search","mobile","female","18-24",1
"New Visitor","Organic Search","mobile","male","18-24",2
"New Visitor","Organic Search","mobile","male","35-44",2
"New Visitor","Organic Search","tablet","male","18-24",1
"Returning Visitor","Direct","desktop","male","25-34",1
"Returning Visitor","Organic Search","desktop","male","18-24",1
"Returning Visitor","Organic Search","desktop","male","25-34",2
"Returning Visitor","Organic Search","desktop","male","35-44",1
"Returning Visitor","Organic Search","mobile","female","35-44",1
"Returning Visitor","Organic Search","mobile","male","25-34",1
"Returning Visitor","Social","desktop","male","25-34",1
"Returning Visitor","Social","mobile","male","25-34",3

関数化しました → ggplot2 で内訳を左から右に詳細化していく棒グラフ - 廿TT

abrahamcow.hatenablog.com