Skip to content

Instantly share code, notes, and snippets.

@madilk
Last active February 27, 2021 19:18
Show Gist options
  • Save madilk/d9cc7c284c7f55aa0efff75558bb9ac6 to your computer and use it in GitHub Desktop.
Save madilk/d9cc7c284c7f55aa0efff75558bb9ac6 to your computer and use it in GitHub Desktop.
R programming: Outliers in ggplot boxplot
#run the GA library
library(googleAnalyticsR)
#authentication token
ga_auth()
#setting up the query
gadata <- google_analytics(viewId = 1234567,
date_range = c(Sys.Date()-100, Sys.Date()-1),
metrics = c("users", "sessions", "pageviews"),
dimensions = c("date", "dayofWeek"),
anti_sample = TRUE)
#examining the first 6 rows as a preview
head(gadata)
#looking at the structure of the data
str(gadata)
#creating boxplot
boxplotchart <- ggplot(gadata,
aes(x=dayofWeek,
y = sessions)) +
geom_boxplot()
boxplotchart +
ylab("Sessions on that day") +
xlab("Day of Week; 0 = Sun, 6 = Sat")
boxplotchart <- ggplot(gadata,
aes(x = as.factor(dayofWeek),
y = sessions)) +
geom_boxplot()
boxplotchart
#table of boxplot data with summary stats
gadata %>%
group_by(as.factor(dayofWeek)) %>%
summarise(Min = min(sessions),
Max = max(sessions),
Median = median(sessions),
IQRange = IQR(sessions))
install.packages("xlsx")
library("xlsx")
write.xlsx(gadata, file = "C:\\Users\\KhanAd\\Dropbox\\blog content\\2018\\052018\\20180526 Day of week boxplot with outlier.xlsx",
sheetName = "Day Of Week", append = FALSE)
gadata %>%
group_by(as.factor(dayofWeek)) %>%
summarise(Min = min(sessions),
Max = max(sessions),
Median = median(sessions),
IQRange = IQR(sessions))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment