R语言训练数据可视化

用R画韦恩图,柱状图,饼图,barplot和violin

2019-07-20  本文已影响112人  gtt儿_生物信息学习

最近需要画一些图,所以把画图的脚本整理了一下,希望对你有帮助。

1.韦恩图venn。2.饼图pie。3.barplot and violin。4.hist

首先加载R包ggplot2和reshape

library(ggplot2)
library(reshape)

若没有这两个包,则用下面的方法安装

source("http://bioconductor.org/biocLite.R")
biocLite("reshape")

安装后需要再次把需要的包加载进去

venn

用pdf把生成的图片保存为pdf

pdf("venn.pdf")
grid.newpage()
a.plot<-draw.pairwise.venn(211, 41, 15, c("a","b"), fill = c("blue", "pink"),cat.cex=2,cex=2)
dev.off()

结果如下图所示:


image.png

pie chart

dt=data.frame(A =c(0.15,0.35,0.5),B=c("a","b","c"))
pdf("pie.pdf")
dt = dt[order(dt$A, decreasing = TRUE),]   ## 用 order() 让数据框的数据按 A 列数据从大到小排序
myLabel = as.vector(dt$B)   
myLabel = paste(myLabel, "(", round(dt$A / sum(dt$A) * 100, 2), "%)        ", sep = "")   
 
p = ggplot(dt, aes(x = "", y = A, fill = B)) + 
   geom_bar(stat = "identity", width = 1) +    
   coord_polar(theta = "y") + 
   labs(x = "", y = "", title = "") + 
   theme(axis.ticks = element_blank()) + 
   theme(legend.title = element_blank(), legend.position = "top") + 
   scale_fill_discrete(breaks = dt$B, labels = myLabel) + 
   theme(axis.text.x = element_blank()) 
p
dev.off()

结果如下图所示:


image.png

bar plot

count<-read.table("barplot.txt")
pdf("barplot.pdf") 
ggplot(data=count,aes(x=V1,y=V3,fill=V2))+geom_bar(stat="identity")
dev.off()

结果如下图所示:


image.png

hist

pdf("hist.pdf")
hist(mydata1$V1,breaks=1000,xlim=c(0,3000),col="red",xlab="depth",main="test")
dev.off()
image.png

boxplot and violin plot

ggplot(data=data_vio_0.1,aes(x=a,y=V5))+geom_boxplot()

结果如下图所示:


image.png
ggplot(data=data_vio_0.1,aes(x=a,y=V5))+geom_violin()

结果图下图所示:


image.png

有任何问题,欢迎留言交流。
欢迎关注微信公众号:生物信息学习。

上一篇 下一篇

猜你喜欢

热点阅读