Thursday, December 8, 2016

Visual 13 - Create Pie Chart

dat1 <- dbGetQuery(conn,"select channel, sum(a.sld_qty)
                  from eipdb_sandbox.ling_sls_brnd_demog a
                  where a.gma_nbr in (2,5)
                  and a.trn_sls_dte between '2016-11-14' AND '2016-12-16'
                  group by 1
                  order by 1;")
dat1$perc <- dat1$sld_qty/sum(dat1$sld_qty)
p1 <- ggplot(dat1, aes(x = factor(1), y =sld_qty, fill = channel)) +
  geom_bar(width = 1, stat = "identity") +
  scale_fill_manual(values = c("red", "blue")) +
  coord_polar(theta="y", start = pi / 3) +
  ##labs(title = "Kohl's Sold Items by Channel") +
  geom_text_repel(aes(label=scales::percent(perc)), size=4.5) + ylab("") + xlab("") +
  theme_void()


dat2 <-dbGetQuery(conn,"select new_ind, sum(a.sld_qty)
                  from eipdb_sandbox.ling_sls_brnd_demog a
                  where a.gma_nbr in (2,5)
                  and a.trn_sls_dte between '2016-11-14' AND '2016-12-16'
                  group by 1
                  order by 1;")
dat2$new_ind <- factor(dat2$new_ind)
dat2$perc <- dat2$sld_qty/sum(dat2$sld_qty)
p2 <- ggplot(dat2, aes(x = "", y =sld_qty, fill = new_ind)) +
  geom_bar(width = 1, stat = "identity") +
  scale_fill_manual(values = c("darkgreen", "orangered", "red")) +
  coord_polar("y", start = pi / 3) +
  ##labs(title = "Kohl's Sold Items by New/Existed Customer") +
  geom_text_repel(aes(label=scales::percent(perc)), size=4.5) + ylab("") + xlab("") +
  theme_void()


dat3 <-dbGetQuery(conn,"select sku_stat_desc, sum(a.sld_qty)
                  from eipdb_sandbox.ling_sls_brnd_demog a
                 where a.gma_nbr in (2,5)
                 and a.trn_sls_dte between '2016-11-14' AND '2016-12-16'
                 group by 1
                 order by 1;")
dat3$perc <- dat3$sld_qty/sum(dat3$sld_qty)
dat3 <- dat3[order(dat3$sld_qty),]
p3 <- ggplot(dat3, aes(x = "", y =sld_qty, fill = sku_stat_desc)) +
  geom_bar(width = 1, stat = "identity") +
  scale_fill_brewer(palette = "Spectral") +
  coord_polar("y", start = pi / 3) +
  ##labs(title = "Kohl's Sold Items by SKU Status") +
  geom_text_repel(aes(label=scales::percent(perc)), size=4.5) + ylab("") + xlab("") +
  theme_void()


grid.arrange(p1,p2,p3, nrow=3, ncol=1)

No comments:

Post a Comment