Skip to content

Instantly share code, notes, and snippets.

@madilk
Last active June 30, 2021 05:43
Show Gist options
  • Save madilk/7ef0052cc7fe18ef6fd22ac70448f176 to your computer and use it in GitHub Desktop.
Save madilk/7ef0052cc7fe18ef6fd22ac70448f176 to your computer and use it in GitHub Desktop.
ggforce r package with google analytics data: geom_mark_hull
#creator of ggforce package: Thomas Lin Pedersen
#https://ggforce.data-imaginist.com/reference/geom_mark_hull.html
library(googleAnalyticsR)
ga_auth(json_file="C:\\location-of-your-json-file-folder\\service-key.json")
#update your view id
viewID <- 12345678
data <- google_analytics(
viewID,
date_range=c("2019-01-01","2021-06-28"),
dimensions=c("month","deviceCategory"),
metrics=c("sessions","pageviewsPerSession")
)
View(data)
library(tidyverse)
##regular scatter plot in ggplot with sessions and pv/session
#hide minor gridlines
# http://www.cookbook-r.com/Graphs/Axes_(ggplot2)/
p <- ggplot(data=data,aes(x=sessions,
y=pageviewsPerSession,
fill=deviceCategory,
colour=deviceCategory,
alpha=0.02))+
geom_point()+
ylim(0,3)+
theme(panel.grid.minor=element_blank())+
scale_alpha(guide="none")
p
##geom_mark_hull in ggplot with sessions and pv/session
#install ggforce
install.packages("ggforce")
library(ggforce)
#install dependency - concaveman package
#install.packages("concaveman")
library(concaveman)
#SO thread on how to remove alpha from legend
#https://stackoverflow.com/questions/11714951/remove-extra-legends-in-ggplot2
h <- ggplot(data=data,aes(x=sessions,
y=pageviewsPerSession,
colour=deviceCategory,
fill=deviceCategory,
filter=deviceCategory=='desktop',
label=deviceCategory,alpha=0.02))+
ylim(0,3)+
theme(panel.grid.minor=element_blank())+
geom_point()+
geom_mark_hull(alpha=0.2)+
scale_alpha(guide="none")
h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment