# ggplot 相关¶

## ggplot同时绘制多个密度图¶

plots <- NULL
for(i in colnames(train)){
plots[[i]] <- ggplot(train) +
geom_density(aes_string(x = i, fill = 'is_black'), alpha = 0.5, show.legend = F) +
xlab("") +
ylab("");
}
plot_grid(plotlist = plots)


## ggplot绘制概率密度图¶

Take the Weibull distribution as an example,

f(x) = \begin{cases} \frac k\lambda \left(\frac x\lambda\right)^{k-1}e^{-(x/\lambda)^k} & x\ge 0\\ 0 & x < 0 \end{cases}

where $\lambda > 0$ is the scale parameter, and $k > 0$ is the shape parameter. And

• if $k=1$, it becomes to the exponential distribution
• if $k=2$, it becomes to the Rayleigh distribution.
d <- seq(0, 5, length.out=10000)
y <- dweibull(d, shape=5, scale=1, log = FALSE)
df <- data.frame(x=d,y)
ggplot(df, aes(x=d, y=y)) +
geom_line(col = "orange") +
ggtitle("Density of Weibull distribution")


h = rweibull(10000, shape=5, scale=1)
ggplot(NULL, aes(x=h)) +
geom_histogram(binwidth=0.01, fill="white", col="red") +
ggtitle("Histogram of Weibull distribution")


ggplot(NULL, aes(x=h)) + geom_density(col = "green")


ggplot(NULL, aes(x=h)) + geom_line(stat = "density", col = "red")


A minor difference is that here is a horizontal line in the above estimated density.

Also refer to Plotting distributions (ggplot2)

## legend设置¶

### 默认情形¶

library(ggplot2)
bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot()
bp


### 自定义图例的顺序¶

# Remove legend for a particular aesthetic (fill)
bp + guides(fill=FALSE)

# It can also be done when specifying the scale
bp + scale_fill_discrete(guide=FALSE)

# This removes all legends
bp + theme(legend.position="none")


bp + scale_fill_discrete(breaks=c("trt1","ctrl","trt2"))


### 颠倒图例的顺序¶

# These two methods are equivalent:
bp + guides(fill = guide_legend(reverse=TRUE))
bp + scale_fill_discrete(guide = guide_legend(reverse=TRUE))

# You can also modify the scale directly:
bp + scale_fill_discrete(breaks = rev(levels(PlantGrowth\$group)))


### 隐藏图例标题¶

# Remove title for fill legend
bp + guides(fill=guide_legend(title=NULL))

# Remove title for all legends
bp + theme(legend.title=element_blank())


### 自定义图例的标题及名称¶




### 图例的整体形状¶

# Title appearance
bp + theme(legend.title = element_text(colour="blue", size=16, face="bold"))

# Label appearance
bp + theme(legend.text = element_text(colour="blue", size = 16, face = "bold"))


bp + theme(legend.background = element_rect())
bp + theme(legend.background = element_rect(fill="gray90", size=.5, linetype="dotted"))


bp + theme(legend.position="top")

# Position legend in graph, where x,y is 0,0 (bottom left) to 1,1 (top right)
bp + theme(legend.position=c(.5, .5))

# Set the "anchoring point" of the legend (bottom-left is 0,0; top-right is 1,1)
# Put bottom-left corner of legend box in bottom-left corner of graph
bp + theme(legend.justification=c(0,0), legend.position=c(0,0))

# Put bottom-right corner of legend box in bottom-right corner of graph
bp + theme(legend.justification=c(1,0), legend.position=c(1,0))


### 隐藏图例的slashes¶

# No outline
ggplot(data=PlantGrowth, aes(x=group, fill=group)) +
geom_bar()

# Add outline, but slashes appear in legend
ggplot(data=PlantGrowth, aes(x=group, fill=group)) +
geom_bar(colour="black")

# A hack to hide the slashes: first graph the bars with no outline and add the legend,
# then graph the bars again with outline, but with a blank legend.
ggplot(data=PlantGrowth, aes(x=group, fill=group)) +
geom_bar() +
geom_bar(colour="black", show.legend=FALSE)


## 数学公式¶

expression(R[group("", list(hat(F),F),"")]^2)


## 多张图片¶

par(mfrow=c(1,2))不起作用，要用到 gridExtra 包，如

library(gridExtra)
plot1 <- qplot(1)
plot2 <- qplot(1)
grid.arrange(plot1, plot2, ncol=2)


## scale_fill_manual 和 scale_color_manual¶

scale_fill_manual(values = c("red", "blue"))


## save¶

NOT png()...dev.off(), use

ggsave("sth.eps",device="eps", width=9)