生信小木屋

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

基本绘图系统(Base Plotting System)

Set or Query Graphical Parameters

par(..., no.readonly = FALSE)

箱线图

图片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

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

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

参考