Quantcast
Channel: グラフ - 廿TT
Viewing all articles
Browse latest Browse all 123

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

$
0
0

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

f:id:abrahamcow:20170508092958p: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,dimentions=dims[1])

out[[1]]<-dat1
for(i in2: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,dimentions=dims[i])}

out_df <- bind_rows(out)

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

ggplot(out_df,aes(x=dimentions,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

abrahamcow.hatenablog.com


Viewing all articles
Browse latest Browse all 123

Trending Articles