今天给大家介绍一个ggplot2包的扩展包——ggprism包,结合该包可绘制GraphPad prism风格样式图形,具体效果我们看图说话!
安装、加载包
rm(list=ls())
# 安装包
install.packages("ggplot2")
install.packages("ggprism")
install.packages("tidyverse")
install.packages("ggpubr")
#加载包
library(ggplot2)
library(ggprism)
library(tidyverse)
library(ggpubr)
准备绘图数据
#自己随机编写的数据
df <- data.frame(
A_1 = c(2,5,6,5,4,8,6,3,8,9),
A_2 = c(5,8,6,3,4,7,9,3,6,4),
B_1 = c(15,10,5,18,12,13,16,14,10,9),
B_2 = c(25,20,23,15,14,24,20,22,25,26),
C_1 = c(1,3,6,5,2,3,6,2,4,1),
C_2 = c(7,8,9,6,7,8,9,6,7,10)
)
#使用tidyverse包对数据进行处理
df <- df %>%
gather(key = 'samples',value = 'values') #gather()函数可以把多列数据合并成一列数据
#添加分组信息
df$group = rep(c("A","B","C"), each = 20)
head(df)#预览数据
绘图
1)ggplot2包绘制的简单图形
p1 <-ggplot(df,aes(samples,values,color=samples,fill=samples))+
geom_bar(stat="summary",fun=mean,position="dodge")+
stat_summary(fun.data = 'mean_sd', geom = "errorbar", width = 0.3,color="black")
p1
2)通过ggprism包调整主题风格
#通过names()函数查看该包包含的主题
names(ggprism_data$themes)
#通过preview_theme()函数可预览每一个主题的样式
preview_theme("diazo")
preview_theme("candy_soft")
preview_theme("blueprint")
preview_theme("summer")
#我们可以通过theme_prism()函数对ggplot2的主题进行设置
p1+theme_prism(palette="summer")
#我们也可以通过ggprism自带调色板对ggplot2绘制图形颜色进行改变
p1+ scale_fill_prism(palette = "summer")
#对主题及颜色同时设置
p1+theme_prism(palette="summer")+
scale_fill_prism(palette = "summer")
#通过??theme_prism查看其中的参数,然后我们进行具体设置
??theme_prism
theme_prism(
palette = "black_and_white",
base_size = 14,
base_family = "sans",
base_fontface = "bold",
base_line_size = base_size/14,
base_rect_size = base_size/14,
axis_text_angle = 0,
border = FALSE)
#应用
p1 + theme_prism(palette="summer",
base_fontface = "plain", # 字体样式
base_family = "serif", # 字体格式
base_size = 16, # 图形字体大小
base_line_size = 0.8, # 坐标轴粗细
axis_text_angle = 45)+ #角度
scale_fill_prism(palette = "summer")