Visualization 1

library(ggplot2)
library(RColorBrewer)
data(diamonds)
diamonds$cut <- paste("Super Dee-Duper",as.character(diamonds$cut))
q <- qplot(cut,carat,data=diamonds,geom="boxplot")+theme_bw()

q + theme(axis.text.x = element_text(angle = 90, hjust = 1))+
  theme(axis.text=element_text(size=16), axis.title=element_text(size=16))

### SOURCE: http://stackoverflow.com/questions/1330989/rotating-and-spacing-axis-labels-in-ggplot2

Visualization 2

stock <- "MSFT"
start.date <- "2006-01-12"
end.date <- Sys.Date()
quote <- paste("http://ichart.finance.yahoo.com/table.csv?s=",
                stock, "&a=", substr(start.date,6,7),
                "&b=", substr(start.date, 9, 10),
                "&c=", substr(start.date, 1,4), 
                "&d=", substr(end.date,6,7),
                "&e=", substr(end.date, 9, 10),
                "&f=", substr(end.date, 1,4),
                "&g=d&ignore=.csv", sep="")    
stock.data <- read.csv(quote, as.is=TRUE)
stock.data <- transform(stock.data,
  week = as.POSIXlt(Date)$yday %/% 7 + 1,
  wday = as.POSIXlt(Date)$wday,
  year = as.POSIXlt(Date)$year + 1900)

library(ggplot2)
ggplot(stock.data, aes(week, wday, fill = Adj.Close))  +theme_bw()+
  geom_tile(colour = "white") + theme(axis.text=element_text(size=18), axis.title=element_text(size=18))+
  scale_fill_gradientn(colours = c("#D61818","#FFAE63","#FFFFBD","#B5E384")) + 
  facet_wrap(~ year, ncol = 1)

#### SOURCE: http://stackoverflow.com/questions/2076370/most-underused-data-visualization

Visualization 3

library("reshape2")
library("ggplot2")
test_data <- data.frame(
var0 = 100 + c(0, cumsum(runif(49, -20, 20))),
var1 = 150 + c(0, cumsum(runif(49, -10, 10))),
date = seq.Date(as.Date("2002-01-01"), by="1 month", length.out=100))


test_data_long <- melt(test_data, id="date")  # convert to long format

ggplot(data=test_data_long,
       aes(x=date, y=value, colour=variable)) +theme_bw()+
       geom_line(size=2)+ theme(axis.text=element_text(size=16), axis.title=element_text(size=18))

#### SOURCE: http://stackoverflow.com/questions/3777174/plotting-two-variables-as-lines-using-ggplot2-on-the-same-graph

Visualization 4

x<-rnorm(300)
y<-rt(300,df=10)
xy<-data.frame(x,y)

scatter <- qplot(x,y, data=xy)  + theme_bw()+
         scale_x_continuous(limits=c(min(x),max(x))) + 
         scale_y_continuous(limits=c(min(y),max(y))) + 
         geom_rug(col=rgb(.5,0,0,alpha=.2))
scatter

### SOURCE: http://stackoverflow.com/questions/8545035/scatterplot-with-marginal-histograms-in-ggplot2

Visualization 5

df <- data.frame(x=rep(1:10,2), y=c(1:10,11:20), 
                 group=c(rep("a",10),rep("b",10)))
head(df)
##   x y group
## 1 1 1     a
## 2 2 2     a
## 3 3 3     a
## 4 4 4     a
## 5 5 5     a
## 6 6 6     a
g <- ggplot(df, aes(x = x, y = y, group = group))+theme_bw()+
  geom_line(size=2)+ theme(axis.text=element_text(size=16), axis.title=element_text(size=18)) 
g <- g + geom_line(aes(colour = group), size=1.2)
g <- g + geom_point(aes(colour = group), alpha = 0.8)
g 

### SOURCE: http://stackoverflow.com/questions/11714951/remove-extra-legends-in-ggplot2

Visualization 6

##   State BCmort year eastWest
## 1    BW   16.5 2010     west
## 2    BY   16.6 2010     west
## 3    BE   15.0 2010     east
## 4    BB   14.4 2010     east
## 5    HB   13.5 2010     west
## 6    HH   17.1 2010     west
ggplot(mort3, aes(x = year, y = BCmort, col = State, linetype = State)) +
  geom_line(size=1.4)+
  theme(axis.text=element_text(size=16), axis.title=element_text(size=16))+
  scale_linetype_manual(values = c(rep("solid", 10), rep("dashed", 6))) +
  scale_x_continuous(breaks=c(1998:2010))+
  scale_color_manual(values = c(brewer.pal(10, "Set3"), brewer.pal(6, "Set3"))) +
  theme_bw()

### SOURCE; http://stackoverflow.com/questions/11344561/controlling-line-color-and-line-type-in-ggplot-legend

Visualization 7

library(ggplot2)
library(plyr)
library(arm)
## Warning: package 'arm' was built under R version 3.2.3
## Loading required package: MASS
## Loading required package: Matrix
## Loading required package: lme4
## 
## arm (Version 1.8-6, built: 2015-7-7)
## 
## Working directory is C:/Users/s-das/Syncplicity Folders/Private/Rpubs1/FEB/4_ggplot2_so1
library(reshape2)
nba <- read.csv("http://datasets.flowingdata.com/ppg2008.csv")
head(nba)
##             Name  G  MIN  PTS  FGM  FGA   FGP FTM FTA   FTP X3PM X3PA
## 1   Dwyane Wade  79 38.6 30.2 10.8 22.0 0.491 7.5 9.8 0.765  1.1  3.5
## 2  LeBron James  81 37.7 28.4  9.7 19.9 0.489 7.3 9.4 0.780  1.6  4.7
## 3   Kobe Bryant  82 36.2 26.8  9.8 20.9 0.467 5.9 6.9 0.856  1.4  4.1
## 4 Dirk Nowitzki  81 37.7 25.9  9.6 20.0 0.479 6.0 6.7 0.890  0.8  2.1
## 5 Danny Granger  67 36.2 25.8  8.5 19.1 0.447 6.0 6.9 0.878  2.7  6.7
## 6  Kevin Durant  74 39.0 25.3  8.9 18.8 0.476 6.1 7.1 0.863  1.3  3.1
##    X3PP ORB DRB TRB AST STL BLK  TO  PF
## 1 0.317 1.1 3.9 5.0 7.5 2.2 1.3 3.4 2.3
## 2 0.344 1.3 6.3 7.6 7.2 1.7 1.1 3.0 1.7
## 3 0.351 1.1 4.1 5.2 4.9 1.5 0.5 2.6 2.3
## 4 0.359 1.1 7.3 8.4 2.4 0.8 0.8 1.9 2.2
## 5 0.404 0.7 4.4 5.1 2.7 1.0 1.4 2.5 3.1
## 6 0.422 1.0 5.5 6.5 2.8 1.3 0.7 3.0 1.8
nba$Name <- with(nba, reorder(Name, PTS))
nba.m <- melt(nba)
## Using Name as id variables
nba.m <- ddply(nba.m, .(variable), transform,
rescale = rescale(value))
(p <- ggplot(nba.m, aes(variable, Name)) + geom_tile(aes(fill = rescale),
colour = "white") + scale_fill_gradient(low = "white",
high = "steelblue")+geom_text(aes(label=round(rescale,1))))

### SOURCE: http://stackoverflow.com/questions/3081066/what-techniques-exists-in-r-to-visualize-a-distance-matrix