今天来一篇R语言绘制柱形图的小推文
生成数据
#加载包
library(export)#导出pdf文件
library(ggplot2)
library(ggsignif)#添加显著性标记
mydata <- data.frame(group=rep(LETTERS[1:2],c(3,3)),
time=rep(c('time1','time2','time3'),2),
value=c(50,55,51,46,72,65),
se=c(6,5,5,4,4,4))

绘图
p1 <- ggplot(mydata, aes(x=time, y = value, fill=group)) #x轴为时间,y轴为value,按照组别进行填充
geom_bar(position='dodge', stat='identity') #position='dodge'表示分组统计,并错开位置避免重叠;stat='identity'以原始值进行统计
scale_fill_manual(values=c('#CCCCCC','#666666',
'#CCCCCC','#666666',
'#CCCCCC','#666666')) #灰色填充
geom_errorbar(aes(ymin=value-se, ymax=value se), #添加标准差
width=0.2,#上下横线宽度为0.2
position=position_dodge(0.9)) #组间距误差线的距离为0.9
# 标签函数:label设置展示标签,vjust设置标签偏移(正上负下),position设置各标签的间距
geom_text(aes(label = value), vjust = 5, colour = 'red', position = position_dodge(0.9), size = 5)
scale_y_continuous(limits = c(0, 100),name='value') #设置y轴的范围,并定义y轴标签
scale_x_discrete(name='时间') #添加x轴标签,x轴的范围也可以设置,同y轴
ggtitle('value在不同时间的各组均值') #设置标题
theme(plot.title=element_text(lineheight=0.8, face='bold', hjust=0.5)) #设置标题,行高0.8,字体加粗,水平居中
theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank(),
panel.background=element_blank(), axis.line=element_line(color='black')) #设置主要次要网格线,背景,坐标轴主题
p1

添加显著性标记
这里的p值是通过之前的检验计算出来的,当然也有直接在绘图中计算的,这里暂时不介绍
p1
#以下部分是组间比较,p值是我随便写的,大家在检验后修改annotations=''就可以
#里面的参数根据自己的数据进行调整
geom_signif(data=mydata,
aes(xmin=0.75, xmax=1.25, annotations='0.33', y_position=60),#xmin=0.75, xmax=1.25设置显著性括号的左右坐标,annotations='0.33'添加的P值,y_position=57)设置高度
textsize = 5, vjust =0.05 , tip_length = c(0.03, 0.2),size = 0.7,# textsize = 5字体大小,vjust = 0.05表示文本标签向上偏移一小段距离,size控制文本标签大小
manual=TRUE) # manual=TRUE手动设置参数
geom_signif(data=mydata,
aes(xmin=1.75, xmax=2.25, annotations='0.03', y_position=80),
textsize = 5, vjust = 0.05, tip_length = c(0.4, 0.03),size = 0.7,
manual=TRUE)
geom_signif(data=mydata,
aes(xmin=2.75, xmax=3.25, annotations='0.02', y_position=75),
textsize = 5, vjust = 0.05, tip_length = c(0.4, 0.03),size = 0.7,
manual=TRUE)

导出pdf文件
graph2pdf(file='D:/柱形图.pdf',font='GB1')
绘制分组堆积柱状图
#生成数据
set.seed(123)
result <- numeric(0) # 创建一个空向量来存储结果
for (i in 1:6) {
random_numbers <- sample(1:25, 2) # 随机生成两个范围为1到50的整数
diff <- 50 - sum(random_numbers) # 计算差值
random_numbers <- c(random_numbers, diff) # 将差值添加到随机数向量中
result <- c(result, random_numbers) # 将生成的向量添加到结果向量中
}
result
mydata1 <- data.frame(group=rep(LETTERS[1:2],c(9,9)),
time=rep(c(rep('time1',3),rep('time2',3),rep('time3',3)),2),
value=rep(c('1','2','3'),6),
count=result)
mydata1$value1 <- c(50,35,15,50,44,15,52,35,15,50,30,15,57,36,15,50,35,15)#该数据主要用于添加值标签时设置的位置参数,应该有专门的设置参数的函数,我没找到,暂时用这种比较笨的方法手动添加
mydata1
mydata1$x<-ifelse(mydata1$time=='time1',1,
ifelse(mydata1$time=='time2',2,3))
df1<-filter(mydata1,group=='A')
df2<-filter(mydata1,group=='B')

绘图
p1 <- ggplot()
geom_bar(data=df1,
aes(x=x,y=count,fill=value),
stat = 'identity',position = 'stack',width=0.3)
geom_text(data = df1,
aes(x=x, y=value1, label=count),
vjust=5, size=5,position=position_dodge(0.9))
geom_bar(data=df2,aes(x=x 0.4,y=count,fill=value),
stat='identity',position = 'stack',width=0.3)
geom_text(data = df2,
aes(x=x 0.4, y=value1, label=count),
vjust=5, size=5,position=position_dodge(0.9))
scale_x_continuous(breaks = c(1.2,2.2,3.2),
labels = c('time1(A vs B)',
'time2(A vs B)',
'time3(A vs B)'),name='时间')
scale_fill_grey()
scale_y_continuous(name='value',limits = c(0,70))
ggtitle(' value在不同时间的各组分布')
theme(plot.title=element_text(lineheight=0.8, face='bold', hjust=0.5))
theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank(),
panel.background=element_blank(), axis.line=element_line(color='black'))
p1

后来找到了不用添加辅助变量添加值标签的方法,但是又遇到了困难,没办法将时间点位于分组柱形图的中间,有懂的大佬可以帮忙解答一下。
p1 <- ggplot(df1,aes(x = time,y = count, fill = value),position = position_dodge(width = 0.1))
# 参数position='stack'表示将不同count对应的数量堆叠在一起,参数width=0.3表示柱子的宽度
geom_bar(stat = 'identity', position = 'stack', width = 0.3)
geom_bar(data=df1,
aes(y=count,fill=value),
stat = 'identity', position = 'stack', width = 0.3)
geom_bar(data=df2,
aes(x=x 0.4,y=count,fill=value),
stat = 'identity',position = 'stack',width=0.3)
# 设置颜色板,使用scale_fill_brewer()函数
scale_fill_brewer(palette = 'Set1')
# 将x轴和y轴翻转,便于展示,将它注释掉,将垂直展示
#coord_flip()
# 添加数值标签,使用geom_text()函数和position_stack()函数设置标签的位置和在图形中的表现形式
geom_text(aes(label = count),
position = position_stack(vjust = 0.5),
colour = 'white',
size = 5)
geom_text(data=df2,aes(x=x 0.4,label = count),
position = position_stack(vjust = 0.5),
colour = 'white',
size = 5)
theme(plot.title=element_text(lineheight=0.8, face='bold', hjust=0.5))
theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank(),
panel.background=element_blank(), axis.line=element_line(color='black'))
p1

垂直展示结果
p1 <- ggplot(df1,aes(x = time,y = count, fill = value),position = position_dodge(width = 0.1))
geom_bar(stat = 'identity', position = 'stack', width = 0.3)
geom_bar(data=df1,
aes(y=count,fill=value),
stat = 'identity', position = 'stack', width = 0.3)
geom_bar(data=df2,
aes(x=x 0.4,y=count,fill=value),
stat = 'identity',position = 'stack',width=0.3)
scale_fill_brewer(palette = 'Set1')
coord_flip() #别的和上面一样,就添加下面一个参数
geom_text(aes(label = count),
position = position_stack(vjust = 0.5),
colour = 'white',
size = 5)
geom_text(data=df2,aes(x=x 0.4,label = count),
position = position_stack(vjust = 0.5),
colour = 'white',
size = 5)
theme(plot.title=element_text(lineheight=0.8, face='bold', hjust=0.5))
theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank(),
panel.background=element_blank(), axis.line=element_line(color='black'))
p1

展示在两个画板上
p1 <- ggplot(mydata1,aes(x = time, group=group,y = count, fill = value),position='dodge')
geom_bar(stat = 'identity', position = 'stack', width = 0.5)
# 设置颜色板,使用scale_fill_brewer()函数
scale_fill_brewer(palette = 'Set1')
# 将x轴和y轴翻转,便于展示,将它注释掉,将垂直展示
coord_flip()
# 添加数值标签,使用geom_text()函数和position_stack()函数设置标签的位置和在图形中的表现形式
geom_text(aes(label = count),
position = position_stack(vjust = 0.5),
colour = 'white',
size = 5)
facet_grid(. ~ group, scales = 'free_x', space = 'free_x')
theme(plot.title=element_text(lineheight=0.8, face='bold', hjust=0.5))
theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank(),
panel.background=element_blank(), axis.line=element_line(color='black'))
p1
###########################################
P1 <- ggplot(mydata1,aes(x=time,y=count,fill=value))
geom_bar(stat='identity',position = 'stack')
#coord_flip()
facet_wrap(~group)
geom_text(aes(y=value1,label=count),vjust=5.9, size=5)
theme_bw()
P1