R三大绘图系统Base、lattice、ggplot

最后发布时间:2023-05-10 10:11:20 浏览量:

生信小木屋

r-graphics
grid
r-graphics-structure
library grid
https://www.jvcasillas.com/base_lattice_ggplot/#ggplot2

基本绘图系统(Base Plotting System)

  • 直观实时的反应绘图和分析数据的逻辑
  • graphics包中
  • plot/hist/boxplot/points/lines/text/title/axis
  • lwd线宽
  • lty 线类型
  • pch点类型
  • par(mfrow=c(1,2)) 全局参数
  • las: Labels axis style
    • always parallel to the axis [default]
    • always horizontal
    • always perpendicular to the axis
    • always vertical
  • marA numerical vector of the form c(bottom,left, top, right)
    • The default is c(5, 4, 4, 2) + 0.1
  • horiz =T:坐标轴翻转

Set or Query Graphical Parameters

par(..., no.readonly = FALSE)
  • las 坐标轴标签 horizontal | vertical
    • numeric in {0,1,2,3}; the style of axis labels.
    • 0: always parallel to the axis [default],
    • 1: always horizontal,
    • 2: always perpendicular to the axis,
    • 3: always vertical.

箱线图

图片alt

图片alt

library(RColorBrewer)
show_col()
boxplot(formula("hsa.mir.381~group"),
        data=df,main="hsa-mir-381", 
        col= brewer.pal(3,"Dark2"))
abline(v=2.4,lty=2)
abline(v=1.4,lty=2)
abline(v=0.6,lty=2)
abline(v=1.6,lty=2)

图片alt

图片alt

lattice

  • 使用一次函数调用
  • 在变量z的不同水平,变量y如何随变量x变化
library(lattice)
set.seed(1)
x<- rnorm(100)
f<- rep(0:1,each=50)
y <- x+f-f*x+rnorm(100,sd=0.5)
f <- factor(f,labels = c("group1","Group2"))
xyplot(y~x | f,layout=(c(1,2)))
xyplot(y~x | f,panel = function(x,y){
  panel.xyplot(x,y)
  panel.abline(y=mean(x),h=mean(y),lty = 2)
  panel.lmline(x,y,col="red")
})

图片alt

图片alt

ggplot

data.frame(feature=colSums(thymus_counts>0), umi_count=colSums(thymus_counts)) |>
    rownames_to_column("cell")|> 
    pivot_longer(!cell,names_to = "type", values_to = "value")|>
    ggplot(aes(x='',y=value)) +
    geom_violin() +
    geom_jitter(colour="blue")+
    facet_wrap(~type,scales="free")

Adding jittered points (a stripchart) to a box plot in ggplot

RColorBrewer

  • seq:连续渐变色
  • div:双向渐变色
  • qual:分类色
library(RColorBrewer)
display.brewer.all(type = "all") #查看所有色板
display.brewer.all(type = "seq") #查看单色渐变色板
display.brewer.all(type = "div") #查看双色渐变色板
display.brewer.all(type = "qual") #查看离散(分类)色板

图片alt

图片alt


图片alt

图片alt


图片alt

图片alt


图片alt

图片alt

brewer.pal.info

图片alt

图片alt

使用image展示

par(mfrow=c(1,2))
library(RColorBrewer)
cols<-brewer.pal(3, "BuGn")
display.brewer.pal(3,"BuGn")
image(volcano,col=colorRampPalette(cols)(20))

图片alt

图片alt

提取某一组色彩主题不连续的颜色

library(RColorBrewer)
library(scales)
a<-brewer.pal(9, "BuGn")
show_col(a[c(1,3,5,7,9)],labels=F)

图片alt

图片alt

library(RColorBrewer)
c<-c(50,30,50,70,90,40)
names(c)<-LETTERS[1:6]
mycolor<-brewer.pal(9,"Greens")
pie(sort(c,decreasing=T),labels=names(c),col=mycolor[c(3,5,5,6,7,9)],clockwise=T,radius=1,border=F)

图片alt

图片alt

library(RColorBrewer)
c<-c(50,30,50,70,90,40)
names(c)<-LETTERS[1:6]
mycolor<-brewer.pal(9,"Greens")
mydata<-data.frame(c)
ggplot(data=mydata,aes(x=factor(1),y=c,fill=factor(c),order=desc(c)))+
  geom_bar(stat="identity",width=1,col="white")+
  coord_polar(theta = "y",start=0)+
  theme(panel.grid = element_blank(),
        panel.background = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_blank())+
  scale_fill_brewer(palette="Greens",labels=c("E", "D", "A","C","F","B"))+
  guides(fill=guide_legend(reverse=TRUE,title=NULL))

图片alt

图片alt


colorRampPalette(brewer.pal(9,"Set1"))(n)
scale_fill_brewer(palette = "Set1")

patchwork

参考