First, I load these library:
library(tidyverse)
library(scales)
Then, I do some data wrangling for the graph
read.csv("I:\\Nhu\\R learning\\Ads_Feb_23.csv") -> adset
#convert the variable into year-month-date format
adset %>% mutate(Day=ymd(Day)) -> adset
#exclude not available observation
adset %>% filter(!is.na(adset$Results)) -> adset
I made a graphing of Facebook ads result by date, age group and
gender
adset %>% ggplot()+
geom_col(aes(x=adset$Day, y = adset$Results, fill = adset$Gender), position = position_dodge(), width = 0.5)+
scale_fill_manual(values = c("#056fa1", "grey", "blue"))+
theme(panel.grid.minor = element_blank())+
labs(title = "Results by date",
x = "Date", y = "Message", fill = "Gender")+
theme(plot.title = element_text(face = "bold", family = "Roboto Slab", hjust = 0))+
facet_wrap(~adset$Age, ncol = 1) -> g1
knitr::knit_print(g1)
%60)
As the contribution of each age group is not clear in the above
chart, I make a pie chart to show age contribution
adset %>% group_by(adset$Age) %>% summarise(tansuat = n()) -> table1
table1 %>% mutate(prop = (tansuat/sum(tansuat)))-> table1
table1 %>% ggplot(aes(x=1, y = prop, fill = table1$`adset$Age`))+
geom_bar(width = 1, stat = "identity")+
geom_text(aes(x = 1, y = table1$prop, label = percent(table1$prop)), position = position_stack(0.5), size = 3)+
coord_polar("y", start=0)+
theme(axis.text = element_blank(), axis.ticks = element_blank())+
labs(title = "Age Contribution",
x = "percent (%)", y = "", fill = "Age group")+
theme(plot.title = element_text(hjust = 0, face = "bold", family = "Roboto Slab"))+
scale_fill_brewer(type = "seq", direction = 1, palette = 1, aesthetics = "fill")+
theme(panel.background = element_blank(),
panel.grid = element_blank()) -> g2
%60)
Finally, I combine the charts for better insight.
library(gridExtra)
grid.arrange(g1, g2, ncol = 2, nrow = 1)
