patternplot包用于绘制有底纹或者图片填充的柱形图、饼图、圈图或箱型图。本次我使用它绘制有底纹及颜色的柱状图,图片如下:
数据如下:
1.读入数据:
mydata <- read.csv("miRNA_length-A.csv")
head(mydata)
x<-factor(mydata$variable, levels=c("20nt-k", "20nt-n", "21nt-k", "21nt-n", "22nt-k", "22nt-n", "23nt-k", "23nt-n", "24nt-k", "24nt-n")) ###使用levels自定义x轴因子的顺序
y<-mydata$value
y
2.作图
p1<-patternbar(mydata,x,y, density = c(30,50,30,50,30,50,30,50,30,50),label.size = 0, ###density设置填充底纹的密度
pattern.type = c('nelines', 'hlines', 'nelines', 'hlines', 'nelines', 'hlines', 'nelines', "hlines",'nelines', 'hlines'),
pattern.line.size = c(3,2,3,2,3,2,3,2,3,2), ###pattern.type设置填充类型,pattern.line.size 设置填充线的宽带,可根据自己的图调整
background.color = c("lightskyblue1","lightskyblue1","lightskyblue1", "lightskyblue1","lightskyblue1","lightskyblue1",
"lightskyblue1","lightskyblue1","lightskyblue1",
"lightskyblue1")) ###background.color设置柱状图的颜色,pattern.color可以设置填充线的颜色
patternplot可供调整的参数还有很多,可以去参考中的官方文档中查找。
附:一些填充类型:
library(reshape2)
library(ggplot2)
library(patternplot)
library(ggbreak)
x<-factor(c('blank', 'bricks', 'vdashes', 'hdashes','crosshatch', 'grid','hlines',
'nelines', 'nwlines','vlines','waves','Rsymbol_0'))
y<-c(1,1,1,1,1,1,1,1,1,1,1,1)
data<-data.frame(x,y)
length(y)
length(x)
p1<-patternbar(data,x,y,
density = c(30,50,50,50,30,50,30,50,30,50, 30,30),label.size = 0,
pattern.type = c('blank', 'bricks', 'vdashes', 'hdashes','crosshatch','grid','hlines',
'nelines', 'nwlines','vlines','waves','Rsymbol_0'),
pattern.line.size = c(3,3,1,1,3,3,3,3,3,3,3,3),
background.color = c("lightskyblue1","lightskyblue1","lightskyblue1",
"lightskyblue1","lightskyblue1","lightskyblue1",
"lightskyblue1","lightskyblue1","lightskyblue1",
"lightskyblue1","lightskyblue1","lightskyblue1","lightskyblue1"),
legend.type='h')+
theme_classic()
p1
ggsave("alltype.png", height=6, width=8, dpi=300)
参考:
官方文档:https://cran.r-project.org/web/packages/patternplot/vignettes/patternplot-intro.html
https://cloud.tencent.com/developer/article/1854767
https://www.rdocumentation.org/packages/patternplot/versions/1.0.0/topics/patternbar
https://zhuanlan.zhihu.com/p/332873029
————————————————————————————————————————————————————————————————————————————————————————————————————————————————
ggbreak用于绘制截断坐标系的图
这个是真的很喜欢!!
我的图片如下:
1.读数据:
data1<-read.csv("CG.csv", sep = ",")
head(data1)
data<-melt(data1) ###整合数据,为簇状柱形图做准备
data
画图
p1<-ggplot(data = data) +geom_bar(aes(x=X,y=value,fill=variable),
stat = "identity", position = position_dodge())
p1 ###画一个柱形图
p2<-p1+scale_y_break(c(0.016, 0.05), scales = "free") +
scale_y_break(c(0.085, 0.35), scales = "free") +
scale_y_break(c(0.4, 0.5), scales = "free")
p2 ####截断y轴
p2 + xlab("Methylation level(%)")+ylab("Percentage of total C")+ theme(axis.text.x = element_text(size = 8, family = "myFont", color = "black", face = "bold", vjust = 0.5, hjust = 0.5, angle = 45))+ theme_classic()+ ggtitle("CHG")
ggsave("allCHG_breaksy.png", width = 5, height = 3, dpi = 300) ###添加坐标轴标签及标题,并导出图片
参考:
chrome-extension://efaidnbmnnnibpcajpcglclefindmkaj/https://cloud.r-project.org/web/packages/ggbreak/ggbreak.pdf
网友评论