Commit 12a85b77 authored by Weigert, Andreas's avatar Weigert, Andreas
Browse files

solution for tutorial 7 added

parent 70b6cf85
...@@ -171,45 +171,98 @@ nl_mailsSend %>% ...@@ -171,45 +171,98 @@ nl_mailsSend %>%
```{r visualization} ```{r visualization}
# exercise 23 # exercise 23
plot(nl_mailsSend$EnergyReport.Cons)
#exercise 24 #exercise 24
plot(nl_mailsSend$EnergyReport.Cons, nl_mailsSend$EnergyReport.PrevCons)
plot(nl_mailsSend$EnergyReport.Cons ~ nl_mailsSend$EnergyReport.PrevCons)
#exercise 25 #exercise 25
plot(nl_mailsSend$EnergyReport.Cons, log="y")
plot(nl_mailsSend$EnergyReport.Cons, nl_mailsSend$EnergyReport.PrevCons, log="xy")
# exercise 26 # exercise 26
boxplot(nl_mailsSend$EnergyReport.Cons)
# exercise 27 # exercise 27
hist(nl_mailsSend$EnergyReport.Cons, breaks = 30)
``` ```
```{r histogram with additional lines - exercise 28 + 29} ```{r histogram with additional lines - exercise 28 + 29}
# exercise 27-29 # exercise 27-29
hist(nl_mailsSend$EnergyReport.Cons,
probability = T, # probability must be used to have the same scale as density
breaks=80, # increases the number of bars of the histogram
ylim = c(0,0.001))
lines(density(nl_mailsSend$EnergyReport.Cons), col=2)
abline(v=quantile(nl_mailsSend$EnergyReport.Cons, probs = c(0.25,0.5,0.75)), col=3)
``` ```
```{r bar and pie charts with colors (exercise 30 extended)} ```{r bar and pie charts with colors (exercise 30 extended)}
library(RColorBrewer)
mycolors <- brewer.pal(4, "Dark2")
barplot(table(as_date(nl_mailsSend$SendDate)), # barplot needs a frequency table
main="Number of emails sent per newsletter", # the plot title
horiz = T, # horizontal barplot
las=1, # aligns the axis labels to the reading direction
cex.names = 0.6, # adjust size of the labels
col=mycolors[c(1,1,1,1,2,3,4)]) # set the colors
pie(table(as_date(nl_mailsSend$SendDate)),
col=mycolors[c(1,1,1,1,2,3,4)],
main="Number of emails sent per newsletter")
``` ```
```{r plot showing email opens and clicks over time} ```{r plot showing email opens and clicks over time}
# identify all opens per day # identify all opens per day
actions_open <- nl_opens %>%
mutate(day_action = as_date(OpenDate)) %>%
group_by(day_action) %>%
summarise(num_opens = n())
# identify all clicks per day # identify all clicks per day
actions_click <- nl_clicks %>%
mutate(day_action = as_date(ClickDate)) %>%
group_by(day_action) %>%
summarise(num_clicks = n())
# combine clicks and opens to one data frame # combine clicks and opens to one data frame
actions_all <- full_join(actions_open, actions_click, by="day_action")
# replace NA values with 0 # replace NA values with 0
actions_all$num_opens <- ifelse(is.na(actions_all$num_opens), 0, actions_all$num_opens)
actions_all$num_clicks <- ifelse(is.na(actions_all$num_clicks), 0, actions_all$num_clicks)
# a simple plot will show a misleading picture # a simple plot will show a misleading picture
plot(actions_all$num_opens ~actions_all$day_action, type="b")
# create rows for all days with no actions # create rows for all days with no actions
days_noaction <- data.frame(
day_action = seq(from = ymd(20170404), to = ymd(20171010), by = "days"),
num_clicks = 0,
num_opens = 0) %>% anti_join(actions_all, by = "day_action")
actions_all <- rbind(actions_all, days_noaction) %>% arrange(day_action)
# find the points in time when the newsletter were send # find the points in time when the newsletter were send
senddates_newsletter <- nl_mailsSend %>%
mutate(nl_uniquename = paste(NewsletterTitle, EnergyReport.Quarter)) %>%
group_by(nl_uniquename) %>% summarise(date_send = mean(SendDate))
plot(actions_all$num_opens ~ actions_all$day_action, type="l",
ylim=c(0,250), xlab="Time", ylab="Number of email opens / clicks")
abline(h=seq(from=0, to=250, by=50), lty="dashed", col="gray", lwd=2)
lines(actions_all$num_clicks ~ actions_all$day_action, col=2)
abline(v=as_date(senddates_newsletter$date_send), col=4, lty=3)
legend("topleft", legend = c("Time of email energy report",
"Number of email opens",
"Number of clicks"), col=c(4,1,2),
lty=c(2,1,1), bg = "White")
``` ```
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment