ggpubr畫圖學習

####ggpubr
library(ggpubr)#load ggpubr
#我的數(shù)據(jù),這種
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,常用的學術(shù)雜志的
#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")#上升排序,區(qū)別于desc,具體看圖演示 
          sort.by.groups=TRUE#按組排序 )
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容