注:此文章整理了常用的统计图绘图代码
所用r包:ggplot2
饼图
p <- ggplot(pdata2,aes(x = factor(1),percent,fill=type))+geom_col(colour = "white")+ coord_polar(theta = "y", start = 0) +
geom_text(aes(label = paste0(round(percent * 100, 2), "%")),
position = position_stack(vjust = 0.5)) + #position_stack 或者position_fill
theme(panel.background = element_blank(),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank())
p
示例
柱形图
简单柱形图
ggplot(data=pdata1,group=period,aes(class))+geom_bar()
table3 <- data.frame(
疾病 = factor(c("总患病率", "冠心病", "脑卒中"), levels = c("总患病率", "冠心病", "脑卒中")),
Count = c(1149, 705, 534),
Percentage = c(9.44, 5.79, 4.39)
)
# 绘制柱状图并在每个柱子上添加患病率标签
ggplot(table3, aes(x = 疾病, y = Percentage, fill = 疾病)) +
geom_bar(stat = "identity", width = 0.5) +
geom_text(aes(label = paste0(Percentage)), vjust = -0.5, size = 5) +
labs(x = "心血管疾病", y = "患病率 (%)") +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1, face = "bold", size = 12), # 修改x轴标签字体大小为12
axis.text.y = element_text(face = "bold", size = 14), # 修改y轴标签字体大小为14
axis.title.x = element_text(face = "bold", size = 16), # 修改x轴标题字体大小为16
axis.title.y = element_text(face = "bold", size = 14), # 修改y轴标题字体大小为14
plot.title = element_text(face = "bold", hjust = 0.5), # 图表标题字体加粗并居中
axis.line = element_line(color = "black", size = 0.8), # 添加轴线,黑色,大小0.8
panel.grid.major = element_blank(), # 移除主要网格线
panel.grid.minor = element_blank(), # 移除次要网格线
panel.border = element_blank(), # 移除面板边框
panel.background = element_blank(), # 移除面板背景
axis.ticks.x = element_line(color = "black", size = 0.5), # 添加x轴刻度线,黑色,大小0.5
axis.ticks.y = element_line(color = "black", size = 0.5), # 添加y轴刻度线,黑色,大小0.5
axis.ticks.length = unit(0.3, "cm") # 设置刻度线长度为0.3厘米
) +
scale_fill_brewer(palette = "Set3") +
scale_y_continuous(limits = c(0, 11), breaks = seq(0, 10, by = 1), expand = c(0, 0)) # 设置y轴刻度范围和刻度位置
复式柱形图
ggplot(data=pdata1,aes(period,score,fill=type))+geom_bar(stat="identity",position = position_dodge())
堆积柱形图
ggplot(data=pdata1,aes(period,score,fill=type))+
#如果是针对分组的柱形图,则position除了可以"identity"(不调整,组内前后重叠)、还包括“stack“(堆积,默认);"fill"(按比例堆积);“dodge“(分散开)
geom_bar(stat="identity",position="stack", color="black", width=0.3,size=0.25)+
# scale_fill_manual(values=c("red", "blue", "green","darkgreen","black"))+
# 添加x,y轴名
labs(x = "术后住院天数",y = "健康评估得分")+
# 坐标轴延伸及标签设置,确保图形元素覆盖至坐标
#离散型变量用scale_**_discrete,连续型用scale_**_continuous
scale_x_discrete(expand = c(0,1),
labels=c("1"="第一天","2"="第二天","3"="第三天","4"="第四天"))+
scale_y_continuous(expand = c(0,0)) +
# 主题类型
theme_classic()+
# 设置主题
theme(panel.background=element_rect(fill="white",colour="black",size=0.25), # 填充框内主题颜色,边框颜色和边框线条粗细
axis.line=element_line(colour="black",size=0.25), # x,y轴颜色,粗细
axis.title=element_text(size=13,color="black"), # x,y轴名设置
axis.text = element_text(size=12,color="black"), # x,y轴文本设置
legend.position= c(0.9,0.7) # 显示图例,c(x,y)这里指将轴默认为1,里面的数字为轴的占比
)+ scale_x_discrete(expand = c(0, 0.8)) # 设置 x 轴长度为数据的80%
示例
折线图
#1.普通单组折线图
ggplot(adam, aes(x = period, y =meanpri_score )) +
geom_line(color = "blue", linewidth = 0.3) +
labs(title = "PRI曲线", x = "住院天数", y = "PRI") +
theme_minimal()