Tuesday, December 1, 2015

Visual 5 - Bar Chart and Stacked Bar Chart

library(plyr)

# Use barplot
mean.prop.sw <- c(0.7, 0.6, 0.67, 0.5, 0.45, 0.48, 0.41, 0.34, 0.5, 0.33)
sd.prop.sw <- c(0.3, 0.4, 0.2, 0.35, 0.28, 0.31, 0.29, 0.26, 0.21, 0.23)
N <- 100
b <- barplot(mean.prop.sw, las = 1, xlab = " ", ylab = " ", col = "grey", cex.lab = 1.7, 
             cex.main = 1.5, axes = FALSE, ylim = c(0, 1))

axis(1, c(0.8, 2, 3.2, 4.4, 5.6, 6.8, 8, 9.2, 10.4, 11.6), 1:10, cex.axis = 1.3)
axis(2, seq(0, 0.8, by = 0.2), cex.axis = 1.3, las = 1)
mtext("Block", side = 1, line = 2.5, cex = 1.5, font = 2)
mtext("Proportion of Switches", side = 2, line = 3, cex = 1.5, font = 2)

l_ply(seq_along(b), function(x) arrows(x0 = b[x], y0 = mean.prop.sw[x], x1 = b[x], y1 = mean.prop.sw[x] + 1.96 * sd.prop.sw[x]/sqrt(N), code = 2, length = 0.1, angle = 90, lwd = 1.5))

# Use barplot
index <- seq(1, length(data2[,1]), by = round(length(data2[,1])/100));
data3 <- data2[index,];
data3 <- data3[sort.list(data3$clickperc),];
barplot(data3$clickperc, name = round(data3$userperc,2),  col = 'blue', main = 'Distribution of Clicks for Comcast 59693',
        xlab = "% Clickers", ylab="% of Clicks", ylim = c(0,1.1));
abline(h = data2$clickperc[6953], col= 'red', lty=3);

#Use ggplot;
 ggplot(data3, aes(userperc)) +
  geom_bar(aes(y = clickperc),stat = "identity", fill = 'blue') +
  geom_abline(intercept = 0, slope =1,  color = 'red', size = 1, lty = 3) +
  ggtitle("Distribution of Random Clicks") +
  ylab("% of Clicks") + xlab("% of Clickers") +
  theme(plot.title = element_text(face = "bold", size = 16)) +
  theme(axis.text.x = element_text(face = "bold", size = 12)) +
  theme(axis.text.y = element_text(face = "bold", size = 12)) +
  theme(axis.title.x = element_text(face = "bold", size = 12)) +
  theme(axis.title.y = element_text(face = "bold", size = 12, angle = 90))

#Use ggplot for barchart;

grp <- rep('1 Large: 10K+', length(conv));
grp[conv <= 10000] <- '2 Medium: 1K - 10K';
grp[conv <= 1000] <- '3 Small: 1K -';

ggplot(data1, aes(y=AUC, x=fnames,fill = grp)) +
  geom_bar() + coord_flip() +
  geom_text(aes(y=AUC, x=fnames, label=round(AUC,2)), hjust=-.2, face = "bold", size = 5, color = "black") +
  ylab("Campaigns") + xlab("AUC") +
  opts(axis.text.x = theme_text(face = "bold", size = 12)) +
  opts(axis.text.y = theme_text(face = "bold", size = 12)) +
  opts(axis.title.x  = theme_text(face = "bold", size = 12)) +
  opts(axis.title.y  = theme_text(face = "bold", size = 12, angle = 90)) +
  opts(title = "AUCs by Bucket Campaigns") +
  opts(plot.title = theme_text(face = "bold", size=14)) +
  opts(legend.position="top") +
  opts(legend.key = theme_rect(colour = NA)) +
  opts(legend.title = theme_blank()) +
  opts(legend.text = theme_text(family = "sans", face = "bold", size

data1=data[, list(army_green_dress=sum(army_green_dress),
            hunter_green_dress=sum(hunter_green_dress),
            coral_dress=sum(coral_dress),
            sage_dress=sum(sage_dress),
            tan_dress=sum(tan_dress)
            )]
data2=data.table(color=colnames(data1),cnt=t(data1))[order(cnt.V1)]
data2$color=factor(data2$color, levels=data2$color)
ggplot(data2, aes(y=cnt.V1, x=color)) +
  geom_bar(stat="identity", fill="blue") +
  ggtitle("Total Searches from 3/27/2016 to 7/16/2016 by Sampled Color") +
  ylab("Total Searches") +
  xlab("Color") +
  geom_text(data=data2, aes(y=cnt.V1, x=color, label=cnt.V1), vjust=-.5, hjust=0, size = 6, color = "black") +
  theme(plot.title = element_text(face = "bold", size = 16)) +
  theme(axis.text.x = element_text(face = "bold", size = 12)) +
  theme(axis.text.y = element_text(face = "bold", size = 12)) +
  theme(axis.title.x = element_text(face = "bold", size = 12)) +
  theme(axis.title.y = element_text(face = "bold", size = 12, angle=90))


## Stack Bar chart
ggplot(data, aes(y=blankimp, x=datekey, fill = targetgrp)) +
  geom_bar(stat = "identity", position = "stack")+
  geom_abline(intercept = mean(data2[,2]), color = 'red', size = 1, lty = 3) +
  annotate("text", x=data2$datekey, y=data2$tot * 1.02, label=paste(round(data2$tot/1000000,2),'M'), size = 6) +
  ggtitle("Daily Impressions by Targeted Groups for Comcast 59693") +
  ylab("Impressions") + xlab("Date") +
  theme(plot.title = element_text(face = "bold", size = 16)) +
  theme(axis.text.x = element_text(face = "bold", size = 12)) +
  theme(axis.text.y = element_text(face = "bold", size = 12)) +
  theme(axis.title.x = element_text(face = "bold", size = 12)) +
  theme(axis.title.y = element_text(face = "bold", size = 12, angle = 90)) +
  theme(legend.position = "top") +
  theme(legend.key = element_rect(colour = NA)) +
  theme(legend.title = element_blank()) +
  theme(legend.text = element_text(family = "sans", face = "bold", size = 12)) +
  scale_x_date(labels = date_format("%m/%d"), breaks = date_breaks("da

No comments:

Post a Comment

Blog Archive