tidyplots绘图,ggplot2二次封装,风格类似Graphpad
基本上代码是原生ggplot2绘图的一半
但是有些细节调整需要引入ggplot2代码
如此处的y轴刻度,同时引入新问题,Y轴Padding再次设为0
管道符操作,可保存为 .jpg . png文件
效果如下:
image.png
代码如下:
library(tidyplots)
library(tidyverse)
# 自定义颜色方案——wlab
color_2_1 <- c("#DEACB9", "#769CBA") #如有多个组,则会填充渐变
color_2_2 <- c("#627597", "#E18E6D")
color_2_3 <- c("#000000", "#E10000") #高对比度,适用无填充,有边框的柱状图
color_2_4 <- c("#FC8C3B", "#68ACD5")
color_3_1 <- c("#EA8379", "#7DAEE0", "#B395BD")
color_3_2 <- c("#299D8F", "#E9C46A", "#D87659")
color_3_3 <- c("#FAC794", "#F58E95", "#AB96DF")
color_4_1 <- c("#769CBA", "#FFD47F", "#BC94C2", "#7A7BBD")
color_4_2 <- c("#C0C0C0", "#42B0D8", "#FBCB92", "#C4DEAE")
color_5_1 <- c("#8ED2C7", "#947FAF", "#DEBCDA", "#F57E74", "#80B3D6")
color_5_2 <- c("#000000", "#FF0000", "#0000FF", "#00C000", "#AD07E3") #高对比
# 读取数据
df1 <- read.csv("ccl2.csv", header = TRUE)
df1
# df1$sample <- factor(df$sample,levels = c("placebo","R40","M15","R40_M15","Mock"))
# sample,count
# placebo,884.87
# placebo,520
# placebo,430
# R40,693.05
# R40,375
# R40,431
# M15,271.09
# M15,237
# M15,327
# R40_M15,149.18
# R40_M15,201
# R40_M15,102
# Mock,10
# Mock,10
# Mock,10
# tidyplots画图,数据变换要先在数据框中完成
# y轴坐标调整需要引用ggplot2代码
p <- df1 |>
mutate(log2y = log2(count))|>
tidyplot(x=sample, y=log2y,color=sample) |>
add_mean_bar(alpha=1,linewidth=2) |>
adjust_colors(new_colors = color_5_1) |>
reorder_x_axis_labels(c("placebo","R40","M15","R40_M15","Mock")) |>
add_sem_errorbar() |>
add_data_points_beeswarm(color ="black") |>
# add_test_pvalue(ref.group = "placebo", hide.ns = T,hide_info = T) |>
add_test_asterisks(ref.group = "placebo", hide.ns = T,hide_info = T) |>
adjust_x_axis(rotate_labels = TRUE) |>
adjust_title("CCL2") |>
adjust_x_axis_title("Drug Treatment") |>
adjust_y_axis_title("Relative mRNA Fold Change") |>
# adjust_legend_title("Drug Treatment") |>
remove_legend() |>
# adjust_y_axis(transform = "log2",limits = c(1, 10000))
adjust_y_axis(limits = c(0, 15))
adjust_caption("color_5_2")
adjust_caption("Relative changes in the mRNA expression of genes")
save_plot("ccl2_invivo.pdf")
# add_test_pvalue(ref.group = "R40", hide.ns = T)
# 设置X轴和Y轴的刻度值和标签
p_with_custom_breaks <- p +
# scale_x_continuous(
# breaks = seq(2, 10, by = 2), # 指定X轴刻度值
# labels = paste("X_", seq(2, 10, by = 2), sep = "") # 指定X轴刻度标签
# ) +
scale_y_continuous(
expand = c(0,0),
breaks = seq(0, 14, by = 2), # 指定Y轴刻度值
# labels = paste("Y_", seq(0, 14, by = 4), sep = ""), # 指定Y轴刻度标签
# 小刻度设置
# minor_breaks = seq(0, 14, by = 1),
# 模拟小刻度,在右侧
# sec.axis = sec_axis(
# trans = ~.,
# breaks = seq(0, 14, by = 1),
# labels = NULL)
)
# 显示图形
print(p_with_custom_breaks)
# 保存PDF
save_plot(p_with_custom_breaks,"mutate_breaks.pdf")