Density plots
根据说明文档,运行代码……
rm(list = ls())
set.seed(1234)
df <- data.frame(sex = factor(rep(c("F","M"), each = 200)),
weight = round(c(rnorm(200,mean=55, sd=5),
rnorm(200,65, sd=5)))
)
head(df)
library(ggplot2)
# Basic density
p <- ggplot(df, aes(x=weight)) +
geom_density()
p
# Add mean line
p+ geom_vline(aes(xintercept=mean(weight)),
color="blue", linetype="dashed", size=1)
# Change line color and fill color
ggplot(df, aes(x=weight))+
geom_density(color="darkblue", fill="lightblue")
# Change line type
ggplot(df, aes(x=weight))+
geom_density(linetype="dashed")
library(plyr)
mu <- ddply(df, "sex", summarise, grp.mean=mean(weight))
head(mu)
# Change density plot line colors by groups
ggplot(df, aes(x=weight, color=sex)) +
geom_density()
# Add mean line
p <- ggplot(df, aes(x=weight, color=sex)) +
geom_density() +
geom_vline(data=mu, aes(xintercept=grp.mean, color=sex),
linetype="dashed")
p
# Use custom color palettes
p+scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# Use brewer color palettes
p+scale_color_brewer(palette="Dark2")
# Use grey scale
p + scale_color_grey() + theme_classic()
#Change density plot fill colors by groups
ggplot(df, aes(x=weight, fill=sex)) +
geom_density()
# Use semi-transparent fill
p<-ggplot(df, aes(x=weight, fill=sex)) +
geom_density(alpha=0.4)
p
p+geom_vline(data=mu, aes(xintercept=grp.mean, color=sex),
linetype="dashed")
# Use custom color palettes
p+scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# use brewer color palettes
p+scale_fill_brewer(palette="Dark2")
# Use grey scale
p + scale_fill_grey() + theme_classic()
# Histogram with density plot
ggplot(df, aes(x=weight)) +
geom_histogram(aes(y=..density..), colour="black", fill="white")+
geom_density(alpha=.2, fill="#FF6666")
# Color by groups
ggplot(df, aes(x=weight, color=sex, fill=sex)) +
geom_histogram(aes(y=..density..), alpha=0.5,
position="identity")+
geom_density(alpha=.2)
网友评论