colorblindr
##
## 4 f r
## 103 106 25
# aesthetic mapping gives multiple groups for each bar
p1 <- ggplot(d) +
aes(x=drv,fill=fl) +
geom_bar()
print(p1)
# stacked, but need to adjust color transparency, which is "alpha"
p1 <- ggplot(d) +
aes(x=drv,fill=fl) +
geom_bar(alpha = 0.3, position="identity")
print(p1)
# better to use position = fill for stacking, but with equivalent height
p1 <- ggplot(d) +
aes(x=drv,fill=fl) +
geom_bar(position="fill")
print(p1)
# best to use position = dodge for multiple bars
p1 <- ggplot(d) +
aes(x=drv,fill=fl) +
geom_bar(position="dodge",color="black",size=1)
print(p1)
# more typical "bar plot" has heights as the values themselves
d_tiny <- tapply(X=d$hwy,INDEX=as.factor(d$fl),FUN=mean) #calculate the means
print (d_tiny)
## c d e p r
## 36.00000 33.60000 13.25000 25.23077 22.99405
## hwy
## c 36.00000
## d 33.60000
## e 13.25000
## p 25.23077
## r 22.99405
## fl hwy
## c c 36.00000
## d d 33.60000
## e e 13.25000
## p p 25.23077
## r r 22.99405
# basic boxplot is simple and informative
p1 <- ggplot(d) +
aes(x=fl,y=hwy,fill=fl) +
geom_boxplot()
print(p1)
# now overlay the raw data
p1 <- ggplot(d) +
aes(x=fl,y=hwy) +
geom_boxplot(fill="thistle",outlier.shape=NA) +
# geom_point()
geom_point(position=position_jitter(width=0.1, height=0.7),
color="grey60",size=2)
print(p1)
### Using Colors
# gray function versus gray colors
# built in greys (0 = black, 100 = white
my_greys <- c("grey20","grey50","grey80")
demoplot(my_greys,"bar")
# converting color plots to black and white
p1 <- ggplot(d,aes(x=as.factor(cyl),y=cty,fill=as.factor(cyl))) + geom_boxplot()
plot(p1)
# default colors look identical in black white
p1_des<- colorblindr::edit_colors(p1, desaturate)
plot(p1_des)
x1 <- rnorm(n=100,mean=0)
x2 <- rnorm(n=100,mean=2.7)
d_frame <- data.frame(v1=c(x1,x2))
lab <- rep(c("Control","Treatment"),each=100)
d_frame <- cbind(d_frame,lab)
str(d_frame)
## 'data.frame': 200 obs. of 2 variables:
## $ v1 : num -0.0306 1.4022 0.166 2.5987 -1.1781 ...
## $ lab: chr "Control" "Control" "Control" "Control" ...
d <- mpg
# --------- discrete classification
# scale_fill_manual for boxplots,bars
# scale_color_manual for points, lines
# boxplot no color
p_fil <- ggplot(d) +
aes(x=as.factor(cyl),y=cty)
p_fil + geom_boxplot()
# boxplot default ggplot fill
p_fil <- ggplot(d) +
aes(x=as.factor(cyl),y=cty,fill=as.factor(cyl)) +
geom_boxplot()
plot(p_fil)
# create custom color palette
my_cols <- c("red","brown","blue","orange")
# boxplot with custom colors for fill
p_fil + scale_fill_manual(values=my_cols)
# scatterplot default ggplot colors
p_col <- ggplot(d) +
aes(x=displ,y=cty,col=as.factor(cyl)) +
geom_point(size=3)
plot(p_col)
# ------- continuous classification (color gradient)
# default color gradient
p_grad <- ggplot(d) +
aes(x=displ,y=cty,col=hwy) +
geom_point(size=3)
plot(p_grad)
## $BottleRocket1
## [1] "#A42820" "#5F5647" "#9B110E" "#3F5151" "#4E2A1E" "#550307" "#0C1707"
##
## $BottleRocket2
## [1] "#FAD510" "#CB2314" "#273046" "#354823" "#1E1E1E"
##
## $Rushmore1
## [1] "#E1BD6D" "#EABE94" "#0B775E" "#35274A" "#F2300F"
##
## $Rushmore
## [1] "#E1BD6D" "#EABE94" "#0B775E" "#35274A" "#F2300F"
##
## $Royal1
## [1] "#899DA4" "#C93312" "#FAEFD1" "#DC863B"
##
## $Royal2
## [1] "#9A8822" "#F5CDB4" "#F8AFA8" "#FDDDA0" "#74A089"
##
## $Zissou1
## [1] "#3B9AB2" "#78B7C5" "#EBCC2A" "#E1AF00" "#F21A00"
##
## $Zissou1Continuous
## [1] "#3A9AB2" "#6FB2C1" "#91BAB6" "#A5C2A3" "#BDC881" "#DCCB4E" "#E3B710"
## [8] "#E79805" "#EC7A05" "#EF5703" "#F11B00"
##
## $Darjeeling1
## [1] "#FF0000" "#00A08A" "#F2AD00" "#F98400" "#5BBCD6"
##
## $Darjeeling2
## [1] "#ECCBAE" "#046C9A" "#D69C4E" "#ABDDDE" "#000000"
##
## $Chevalier1
## [1] "#446455" "#FDD262" "#D3DDDC" "#C7B19C"
##
## $FantasticFox1
## [1] "#DD8D29" "#E2D200" "#46ACC8" "#E58601" "#B40F20"
##
## $Moonrise1
## [1] "#F3DF6C" "#CEAB07" "#D5D5D3" "#24281A"
##
## $Moonrise2
## [1] "#798E87" "#C27D38" "#CCC591" "#29211F"
##
## $Moonrise3
## [1] "#85D4E3" "#F4B5BD" "#9C964A" "#CDC08C" "#FAD77B"
##
## $Cavalcanti1
## [1] "#D8B70A" "#02401B" "#A2A475" "#81A88D" "#972D15"
##
## $GrandBudapest1
## [1] "#F1BB7B" "#FD6467" "#5B1A18" "#D67236"
##
## $GrandBudapest2
## [1] "#E6A0C4" "#C6CDF7" "#D8A499" "#7294D4"
##
## $IsleofDogs1
## [1] "#9986A5" "#79402E" "#CCBA72" "#0F0D0E" "#D9D0D3" "#8D8680"
##
## $IsleofDogs2
## [1] "#EAD3BF" "#AA9486" "#B6854D" "#39312F" "#1C1718"
##
## $FrenchDispatch
## [1] "#90D4CC" "#BD3027" "#B0AFA2" "#7FC0C6" "#9D9C85"
##
## $AsteroidCity1
## [1] "#0A9F9D" "#CEB175" "#E54E21" "#6C8645" "#C18748"
##
## $AsteroidCity2
## [1] "#C52E19" "#AC9765" "#54D8B1" "#b67c3b" "#175149" "#AF4E24"
##
## $AsteroidCity3
## [1] "#FBA72A" "#D3D4D8" "#CB7A5C" "#5785C1"
#### Making a heat map
xVar <- 1:30
yVar <- 1:5
myData <- expand.grid(xVar=xVar,yVar=yVar)
head(myData)
## xVar yVar
## 1 1 1
## 2 2 1
## 3 3 1
## 4 4 1
## 5 5 1
## 6 6 1
## xVar yVar zVar
## 1 1 1 1.930717
## 2 2 1 4.783829
## 3 3 1 4.301021
## 4 4 1 3.626993
## 5 5 1 6.506057
## 6 6 1 5.774694
# default gradient colors in ggplot
p4 <- ggplot(myData) +
aes(x=xVar,y=yVar,fill=zVar) +
geom_tile()
print(p4)