1.Histogram plots
1.1 语法
geom_freqpoly( mapping = NULL, data = NULL, stat = "bin", position = "identity", ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
geom_histogram( mapping = NULL, data = NULL, stat = "bin", position = "stack", ..., binwidth = NULL, bins = NULL, na.rm = FALSE, orientation = NA, show.legend = NA, inherit.aes = TRUE )
stat_bin( mapping = NULL, data = NULL, geom = "bar", position = "stack", ..., binwidth = NULL, bins = NULL, center = NULL, boundary = NULL, breaks = NULL, closed = c("right", "left"), pad = FALSE, na.rm = FALSE, orientation = NA, show.legend = NA, inherit.aes = TRUE )
1.2 基础直方图
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, mean=65, sd=5)))
)
head(df)
library(ggplot2)
# 基础直方图
p1 <- ggplot(df, aes(x=weight)) + geom_histogram()
# 修改bins宽度,bin为容器的意思
p2 <- ggplot(df, aes(x=weight)) +
geom_histogram(binwidth=1)
# 修改颜色
p3 <- ggplot(df, aes(x=weight)) +
geom_histogram(color="black", fill="white")
ggarrange(p1,p2,p3,nrow = 1)
1.3 在直方图上添加平均线和密度图
# 直方图以密度绘制,而不是在y轴上计数
# 用透明密度图覆盖,alpha值控制透明度
# 添加平均线
p4 <- p3 + geom_vline(aes(xintercept=mean(weight)),
color="blue", linetype="dashed", size=1)
# 带有密度分布的直方图
p5 <- ggplot(df, aes(x=weight)) +
geom_histogram(aes(y=..density..), colour="black", fill="white")+
geom_density(alpha=.2, fill="#FF6666")
ggarrange(p4,p5,nrow = 1)
1.4 更改直方图绘图线的类型和颜色
# 更改线的颜色及填充色
p6 <- ggplot(df, aes(x=weight))+
geom_histogram(color="darkblue", fill="lightblue")
# 修改线型
p7 <- ggplot(df, aes(x=weight))+
geom_histogram(color="black", fill="lightblue",
linetype="dashed")
ggarrange(p6,p7)
1.5 按组更改直方图图的颜色
# 计算每组的平均值:
# plyr包用于计算每组的平均重量
library(plyr)
mu <- ddply(df, "sex", summarise, grp.mean=mean(weight))
head(mu)
# 按组更改直方图绘图线颜色
p8 <- ggplot(df, aes(x=weight, color=sex)) +
geom_histogram(fill="white")
# 叠加直方图
p9 <-ggplot(df, aes(x=weight, color=sex)) +
geom_histogram(fill="white", alpha=0.5, position="identity")
# 交错直方图
p10 <-ggplot(df, aes(x=weight, color=sex)) +
geom_histogram(fill="white", position="dodge")+
theme(legend.position="top")
# 添加平均线
p11 <- ggplot(df, aes(x=weight, color=sex)) +
geom_histogram(fill="white", position="dodge")+
geom_vline(data=mu, aes(xintercept=grp.mean, color=sex),
linetype="dashed")+
theme(legend.position="top")
ggarrange(p8,p9,p10,p11,nrow = 1)
1.6 使用以下函数手动更改直方图绘图线的颜色:
# scale_color_manual():使用自定义颜色
# scale_color_brewer():使用RColorBrewer包中的调色板
# scale_color_grey():使用灰色调色板
# 自定义调色板
p12 <- p11 + scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# brewer调色板
p13 <- p11 + scale_color_brewer(palette="Dark2")
# 灰度
p14 <- p11 + scale_color_grey() + theme_classic() +
theme(legend.position="top")
ggarrange(p12,p13,p14,nrow = 1)
1.7 按组修改填充色
# 按组修改填充色
p15 <- ggplot(df, aes(x=weight, fill=sex, color=sex)) +
geom_histogram(position="identity")
# 使用半透明填充
p16 <- ggplot(df, aes(x=weight, fill=sex, color=sex)) +
geom_histogram(position="identity", alpha=0.5)
# 添加平均线
p17 <- p16 + geom_vline(data=mu, aes(xintercept=grp.mean, color=sex),
linetype="dashed")
# 使用自定义调色板
p18 <- p16 + scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# brewer调色板
p19 <- p16 + scale_color_brewer(palette="Dark2")+
scale_fill_brewer(palette="Dark2")
# 灰度
p20 <- p16 + scale_color_grey()+scale_fill_grey() +
theme_classic()
ggarrange(p15,p16,p17,p18,p19,p20,nrow=2,ncol=3)
1.8 更改图例位置
# 更改图例位置 legend.position : “left”,“top”, “right”, “bottom”,or vector
p21 <- p16 + theme(legend.position="top")
p22 <-p16 + theme(legend.position="bottom")
p23 <-p16 + theme(legend.position="none") # 移除图例
ggarrange(p21,p22,p23,nrow = 1)
1.9 分面
# 分面
p24 <- ggplot(df, aes(x=weight))+
geom_histogram(color="black", fill="white")+
facet_grid(sex ~ .)
# 添加平均线
p25 <- p24 + geom_vline(data=mu, aes(xintercept=grp.mean, color="red"),
linetype="dashed")
ggarrange(p24,p25)
1.10 定制直方图
# 基础直方图
p26 <- ggplot(df, aes(x=weight, fill=sex)) +
geom_histogram(fill="white", color="black")+
geom_vline(aes(xintercept=mean(weight)), color="blue",
linetype="dashed")+
labs(title="Weight histogram plot",x="Weight(kg)", y = "Count")+
theme_classic()
# 按组更改线条颜色
p27 <- ggplot(df, aes(x=weight, color=sex, fill=sex)) +
geom_histogram(position="identity", alpha=0.5)+
geom_vline(data=mu, aes(xintercept=grp.mean, color=sex),
linetype="dashed")+
scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
labs(title="Weight histogram plot",x="Weight(kg)", y = "Count")+
theme_classic()
# 按组更改线条颜色
p28 <- ggplot(df, aes(x=weight, color=sex, fill=sex)) +
geom_histogram(aes(y=..density..), position="identity", alpha=0.5)+
geom_density(alpha=0.6)+
geom_vline(data=mu, aes(xintercept=grp.mean, color=sex),
linetype="dashed")+
scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
labs(title="Weight histogram plot",x="Weight(kg)", y = "Density")+
theme_classic()
ggarrange(p26,p27,p28,nrow = 1)
# 手动更改线条颜色:
p29 <- ggplot(df, aes(x=weight, color=sex)) +
geom_histogram(fill="white", position="dodge")+
geom_vline(data=mu, aes(xintercept=grp.mean, color=sex),
linetype="dashed")
# Continuous colors
p30 <- p29 + scale_color_brewer(palette="Paired") +
theme_classic()+theme(legend.position="top")
# Discrete colors
p31 <- p29 + scale_color_brewer(palette="Dark2") +
theme_minimal()+theme_classic()+theme(legend.position="top")
# Gradient colors
p32 <- p29 + scale_color_brewer(palette="Accent") +
theme_minimal()+theme(legend.position="top")
ggarrange(p29,p30,p31,p32,nrow = 1)
2.Bar plots
2.1 geom_bar()语法
geom_bar( mapping = NULL, data = NULL, stat = "count", position = "stack", ..., width = NULL, na.rm = FALSE, orientation = NA, show.legend = NA, inherit.aes = TRUE )
geom_col( mapping = NULL, data = NULL, position = "stack", ..., width = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
stat_count( mapping = NULL, data = NULL, geom = "bar", position = "stack", ..., width = NULL, na.rm = FALSE, orientation = NA, show.legend = NA, inherit.aes = TRUE )
2.1 基础条形图
# 加载数据
df <- data.frame(dose=c("D0.5", "D1", "D2"),
len=c(4.2, 10, 29.5))
head(df)
library(ggplot2)
# 基础条形图
p <- ggplot(data=df, aes(x=dose, y=len)) +
geom_bar(stat="identity")
# 水平条形图
p1 <- p + coord_flip()
ggarrange(p,p1)
# 更改条的宽度和颜色:
# 更改条的宽度
p2 <- ggplot(data=df, aes(x=dose, y=len)) +
geom_bar(stat="identity", width=0.5)
# 改变颜色
p3 <- ggplot(data=df, aes(x=dose, y=len)) +
geom_bar(stat="identity", color="blue", fill="white")
# 最小主题+蓝色填充颜色
p4 <- ggplot(data=df, aes(x=dose, y=len)) +
geom_bar(stat="identity", fill="steelblue")+
theme_minimal()
# 选择要显示的项目
p5 <- p + scale_x_discrete(limits=c("D0.5", "D2"))
ggarrange(p2,p3,p4,p5,nrow = 1)
2.2 带标签的条形图
# 条形外侧标签
p6 <- ggplot(data=df, aes(x=dose, y=len)) +
geom_bar(stat="identity", fill="steelblue")+
geom_text(aes(label=len), vjust=-0.3, size=3.5)+
theme_minimal()
# 条形内部标签
p7 <- ggplot(data=df, aes(x=dose, y=len)) +
geom_bar(stat="identity", fill="steelblue")+
geom_text(aes(label=len), vjust=1.6, color="white", size=3.5)+
theme_minimal()
ggarrange(p6,p7,nrow = 1)
2.3 计数条形图
# 计数条形图
head(mtcars)
# 不要将变量映射到y
p8 <- ggplot(mtcars, aes(x=factor(cyl))) +
geom_bar(stat="count", width=0.7, fill="steelblue")+
theme_minimal()
p8
2.4 按组更改条形图颜色
# 按组更改条形图颜色
# 更改轮廓颜色
# 按组更改条形图线条颜色
p9 <- ggplot(df, aes(x=dose, y=len, color=dose)) +
geom_bar(stat="identity", fill="white")
# 自定义调色板
p10 <- p9 + scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# brewer调色板
p11 <- p9 + scale_color_brewer(palette="Dark2")
# 灰度
p12 <- p9 + scale_color_grey() + theme_classic()
ggarrange(p9,p10,p11,p12,nrow = 1)
# 更改填充颜色
# 按组更改填充色
p13 <- ggplot(df, aes(x=dose, y=len, fill=dose)) +
geom_bar(stat="identity")+theme_minimal()
# 自定义调色板
p14 <- p13 + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# brewer调色板
p15 <- p13 + scale_fill_brewer(palette="Dark2")
# 灰度
p16 <- p13 + scale_fill_grey()
ggarrange(p13,p14,p15,p16,nrow = 1)
2.6 修改图例
# 使用黑色轮廓色:
p17 <- ggplot(df, aes(x=dose, y=len, fill=dose))+
geom_bar(stat="identity", color="black")+
scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
theme_minimal()
# 更改图例位置
# 将条形填充颜色更改为蓝色
p18 <- p17 + scale_fill_brewer(palette="Blues")
p19 <- p17 + theme(legend.position="top")
p20 <- p17 + theme(legend.position="bottom")
# 移除图例
p21 <- p17 + theme(legend.position="none")
# 更改图例中的项目顺序
p22 <- p17 + scale_x_discrete(limits=c("D2", "D0.5", "D1"))
ggarrange(p17,p18,p19,p20,p21,p22,nrow = 2,ncol = 3)
2.7 具有多个组的条形图
# 具有多个组的条形图
df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
dose=rep(c("D0.5", "D1", "D2"),2),
len=c(6.8, 15, 33, 4.2, 10, 29.5))
head(df2)
# 具有多个组的堆叠条形图
p23 <- ggplot(data=df2, aes(x=dose, y=len, fill=supp)) +
geom_bar(stat="identity")
# position=position_dodge()
p24 <- ggplot(data=df2, aes(x=dose, y=len, fill=supp)) +
geom_bar(stat="identity", position=position_dodge())
# 手动修改颜色
p25 <- ggplot(data=df2, aes(x=dose, y=len, fill=supp)) +
geom_bar(stat="identity", color="black", position=position_dodge())+
theme_minimal()
# 自定义颜色
p26 <- p25 + scale_fill_manual(values=c('#999999','#E69F00'))
# brewer调色板
p27 <- p25 + scale_fill_brewer(palette="Blues")
# 添加标签
p28 <- ggplot(data=df2, aes(x=dose, y=len, fill=supp)) +
geom_bar(stat="identity", position=position_dodge())+
geom_text(aes(label=len), vjust=1.6, color="white",
position = position_dodge(0.9), size=3.5)+
scale_fill_brewer(palette="Paired")+
theme_minimal()
ggarrange(p23,p24,p25,p26,p27,p28,nrow = 2,ncol=3)
library(plyr)
# 排序
df_sorted <- arrange(df2, dose, supp)
head(df_sorted)
# 计算每个剂量的len的累积总和
df_cumsum <- ddply(df_sorted, "dose",
transform, label_ypos=cumsum(len))
head(df_cumsum)
# 绘图
p29 <- ggplot(data=df_cumsum, aes(x=dose, y=len, fill=supp)) +
geom_bar(stat="identity")+
geom_text(aes(y=label_ypos, label=len), vjust=1.6,
color="white", size=3.5)+
scale_fill_brewer(palette="Paired")+
theme_minimal()
# 如果要将标签放置在条形图的中间,则必须按如下所示修改累积和:
df_cumsum <- ddply(df_sorted, "dose",
transform,
label_ypos=cumsum(len) - 0.5*len)
# Create the barplot
p30 <- ggplot(data=df_cumsum, aes(x=dose, y=len, fill=supp)) +
geom_bar(stat="identity")+
geom_text(aes(y=label_ypos, label=len), vjust=1.6,
color="white", size=3.5)+
scale_fill_brewer(palette="Paired")+
theme_minimal()
ggarrange(p29,p30)
2.8 具有数字X轴的条形图
# 具有数字X轴的条形图
# Create some data
df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
dose=rep(c("0.5", "1", "2"),2),
len=c(6.8, 15, 33, 4.2, 10, 29.5))
head(df2)
# x轴视为连续变量
df2$dose <- as.numeric(as.vector(df2$dose))
p31 <- ggplot(data=df2, aes(x=dose, y=len, fill=supp)) +
geom_bar(stat="identity", position=position_dodge())+
scale_fill_brewer(palette="Paired")+
theme_minimal()
# 轴视为离散变量
df2$dose<-as.factor(df2$dose)
p32 <- ggplot(data=df2, aes(x=dose, y=len, fill=supp)) +
geom_bar(stat="identity", position=position_dodge())+
scale_fill_brewer(palette="Paired")+
theme_minimal()
ggarrange(p31,p32)
2.9 带误差条的条形图
# 计算每组平均值和标准偏差的功能
# 数据:数据框
# varname:包含要汇总的变量的列的名称
# groupnames:用作分组变量的列名称的向量
data_summary <- function(data, varname, groupnames){
require(plyr)
summary_func <- function(x, col){
c(mean = mean(x[[col]], na.rm=TRUE),
sd = sd(x[[col]], na.rm=TRUE))
}
data_sum<-ddply(data, groupnames, .fun=summary_func,
varname)
data_sum <- rename(data_sum, c("mean" = varname))
return(data_sum)
}
df3 <- data_summary(ToothGrowth, varname="len",
groupnames=c("supp", "dose"))
# 将剂量转换为因子变量
df3$dose=as.factor(df3$dose)
head(df3)
# 平均值的标准偏差作为误差线
p33 <- ggplot(df3, aes(x=dose, y=len, fill=supp)) +
geom_bar(stat="identity", position=position_dodge()) +
geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=.2,
position=position_dodge(.9))
p34 <- p33 + scale_fill_brewer(palette="Paired") + theme_minimal()
ggarrange(p33,p34)
2.10 自定义条形图
# 自定义条形图
# 按组更改颜色
# 添加错误栏
p35 <- p33 + labs(title="Plot of length per dose",
x="Dose (mg)", y = "Length")+
scale_fill_manual(values=c('black','lightgray'))+
theme_classic()
# Greens
p36 <- p33 + scale_fill_brewer(palette="Greens") + theme_minimal()
# Reds
p37 <- p33 + scale_fill_brewer(palette="Reds") + theme_minimal()
ggarrange(p35,p36,p37,nrow = 1)
Reference
1.ggplot2 histogram plot : Quick start guide - R software and data visualization
2.ggplot2 barplots : Quick start guide - R software and data visualization