绘制密度图
ggplot() + geom_density(data=, aes(x=), color=, fill=)
ggplot() + geom_density(data=, aes(x=, color=), size=)
ggplot() + geom_density(data=, aes(x=, fill=), alpha=)
color=
: 轮廓颜色
fill=
: 填充颜色
size=
: 线宽度
alpha=
: 填充颜色的透明度,0
为完全透明,1
为完全不透明
1. 简单密度图(仅一组数据)
kk <- as.data.frame(rnorm(10000))
colnames(kk) <- "pp"
ggplot()+geom_density(data=kk, aes(x=pp), color = "black", fill = "gray") '#示例一
ggplot()+geom_density(data=kk, aes(x=pp), color = "black", fill = "gray")+theme_classic() #示例二
示例一
示例二
2. 组合密度(两组或者以上数据)
kk <- as.data.frame(rnorm(10000))
kk[,2] <- "type_1"
colnames(kk) <- c("column_1", "column_2")
pp <- as.data.frame(rnorm(7000)+2)
pp[,2] <- "type_2"
colnames(pp) <- c("column_1", "column_2")
ff <- as.data.frame(rnorm(8000)-3)
ff[,2] <- "type_3"
colnames(ff) <- c("column_1", "column_2")
tt <- rbind(kk, pp, ff)
tt[,2] <- as.factor(tt[,2])
ggplot()+geom_density(data = tt, aes(x=column_1, color=column_2), size=1.5) '#示例三
ggplot()+geom_density(data = tt, aes(x=column_1, fill=column_2), alpha=0.3) #示例四
示例 三
示例四