ggpubr画图学习
2020-04-13 本文已影响0人
杏仁核
####ggpubr
library(ggpubr)#load ggpubr
#我的数据,这种
p<-ggboxplot(cc,x="style",y="FAT4",color = "group")
p
image.png
image.png
#add grids by the function grids()#
#1、背景虚线
p+grids(linetype="dashed")
#add panel borders lines by the function border()
#2、周围有框框
p+border("black")
#3、change background color背景颜色
p+bgcolor("gray")+border("gray")
#4、ggpar改坐标,标签,表头等等
p2 <- ggpar(p,
title = "Box plot created with ggpubr",
subtitle = "Length by dose",
caption = "Source: ggpubr",
xlab = "Dose (mg)",
ylab = "Teeth length",
legend.title = "Dose (mg)")
p2#颜色,字体大小
ggpar(p2,
font.title = c(14, "bold.italic", "red"),
font.subtitle = c(10, "orange"),
font.caption = c(10, "orange"),
font.x = c(14, "blue"),
font.y = c(14, "#993333"))
#或者直接用font
p2+
font("title", size = 14, color = "red", face = "bold.italic")+
font("subtitle", size = 10, color = "orange")+
font("caption", size = 10, color = "orange")+
font("xlab", size = 12, color = "blue")+
font("ylab", size = 12, color = "#993333")
##一次性
ggpar(p,
title = "Plot of length \n by dose",#\n是下一段
xlab = "Dose (mg)",
legend.title = "Dose (mg)",
font.title = c(14, "bold.italic", "red"),
font.x = c(14, "bold", "#2E9FDF"),
font.y = c(14, "bold", "#E7B800"))
image.png
#5、图例位置
ggpar(p,
legend = "right", legend.title = "Dose (mg)")+
font("legend.title", color="blue", face = "bold")+
font("legend.text", color = "red")
#6、配色
#use custom color palette
ggpar(p, palette = c("#00AFBB", "#E7B800", "#FC4E07"))#palette
#use the ggsci palette
ggpar(p, palette = "npg")#nature#还有jco,常用的学术杂志的
#jco color palette
p+color_palette("jco")#还有fill_palette
#first create a scatter plot
#颜色梯度
p3 <- ggscatter(cc, x="style", y="FAT4", color="FAT4", size = 2)
p3
#change the gradient color
#use one custom color,换颜色?
#gradient_fill差不多
p3+gradient_color("red")
#use two colors
p3+gradient_color(c("blue", "red"))
#use the RColorBrewer palette
p3+gradient_color("RdYlBu")
image.png
#7、修改坐标轴
#change y axis limits
ggpar(p, ylim = c(0, 10))
#change y axis cale to log2
ggpar(p, yscale = "log2")
#format axis scale
ggpar(p, yscale = "log2", format.scale = TRUE)#format.scale=TRUE说明y轴刻度也会scale
image.png
#也可以直接用yscale()
p+yscale("log2", .format = TRUE)
#change the font of x and y axis texts
#rotate x and y texts
p+
font("xy.text", size = 12, color = "blue", face = "bold")+
rotate_x_text(45)+
rotate_y_text(45)
image.png
#remove ticks and axis texts
p+rremove("ticks")+
rremove("axis.text")
#8、修改主题默认主题为theme_pubr(),
library(ggthemes)
p+ggthemes::theme_economist()
p <- ggboxplot(ToothGrowth, x="dose", y="len", ggtheme = theme_igray())
p
#移除用rremove
ggdensity(cc,x="style",color = "group",add = "mean", rug = TRUE,
palette = "jco")#杂志jco的配色
sort.val = "asc")#上升排序,区别于desc,具体看图演示
sort.by.groups=TRUE#按组排序 )