Thursday, December 3, 2015

Visual 9 - Heat Map in R

tmp1 <- table(visit_cmatrix[,2])
tmp1 <- data.frame(names(tmp1), cbind(tmp1))
colnames(tmp1) <- c('WebsiteCategory','1')

for ( i in 3:21) {
tmp <- table(visit_cmatrix[,i])
tmp <- data.frame(names(tmp), cbind(tmp))
colnames(tmp) <- c('WebsiteCategory', paste("", i-1, sep = ""))
tmp1 <- merge(tmp1, tmp, by = 'WebsiteCategory')
}
tmp1 <- tmp1[sort.list(tmp1[,2], decreasing = F),]
tmp1[,1] <- factor(tmp1[,1], levels = tmp1[,1])
tmp2 <- melt(tmp1, id ='WebsiteCategory')
tmp3 <- ddply(tmp2, .(variable), transform, rescale = rescale(value))

ggplot(tmp3, aes(variable, WebsiteCategory)) +
    geom_tile(aes(fill = rescale, order = rescale), colour = "white") +
    scale_fill_gradient(name = "Rescale of \n # users", low = "lightblue", high = "red")  +
    xlab("Visits") + ylab("Website Category") +
    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 = "HeatMap of Multiple Visit Categories") +
    opts(plot.title = theme_text(face = "bold", size=14))

No comments:

Post a Comment

Blog Archive