服务热线
178 0020 3020
##作业1自定义主题练习 library(ggplot2) mytheme_volcano <- theme_classic() + theme(panel.grid = element_blank(), #隐藏网格线 legend.position = "no", #移除图例 plot.caption = element_text(face = "bold.italic"), #caption签名element_text字体face格式设置为黑体加斜体 panel.border=element_rect(fill='transparent', color='black')) #加上上右边界线封闭图形 ggplot(mtcars, aes(factor(cyl))) + geom_bar(aes(fill = factor(gear)), width = 0.6) + labs(title = "title", subtitle = "subtitle", caption = "caption", x = "cyl", fill = "gear") + mytheme_volcano ##作业2画火山图并利用自定义主题 setwd("F:\\Doctor\\R语言学习\\互助小组\\第7期作业\\R2-第七期作业-火山图") install.packages("latex2exp") #安装"latex2exp"包,以便log等数学符号输出 library(latex2exp) library(ggplot2) myvolcano <- read.csv("nature23643-s4.csv") myvolcano$significant <- as.factor(ifelse(-log2(myvolcano$q_value)>5, ifelse(abs(log2(myvolcano$FC)) >1,"yes" ,"not"),"not"))#区分表达差异显著基因 mysig <- subset(myvolcano,significant == "yes")#提取差异显著基因子集 ydrop = 0.6 #用于设置箭头直线和三角的y轴位置 textdrop = 1.2 #用于设置“...regulated"位置 ggplot(myvolcano,aes(x = log2(FC) , y = -log2(q_value),size = significant)) + geom_point(col = "grey70",alpha = 1) + #画基本灰色点图 scale_size_manual(values = c(2,0)) + #自定义点的大小,并把显著差异的点的大小设置为0 geom_point(data = mysig,aes(x = log2(FC),y = -log2(q_value), color=锘縂ene.ID),size = 4) + #画差异基因点图(本来color应该=Gene ID,但数据读取乱码,所以用乱码代替) geom_text(data = mysig,aes(label = 锘縂ene.ID),size =4,hjust=0.5,vjust =1.5,col = "black", fontface = "italic") + #给差异基因加上名字 annotate("segment", x = 1 , xend = 2.5, y = ydrop,yend = ydrop ,color = "black", size = 1,arrow = arrow(angle = 18,ends = "last",type = "closed")) + #画箭头 annotate("segment", x = -1 , xend = -2.5, y = ydrop,yend = ydrop,color = "black", size = 1,arrow = arrow(angle = 18,ends = "last",type = "closed")) + annotate("text",x = 1.7,y = textdrop,label = "Upregulated",size = 4.5) + #箭头标注 annotate("text",x = -1.7,y = textdrop,label = "Downregulated",size = 4.5) + geom_vline(xintercept = 0,linetype = 2 ,size= rel(0.8)) + # 加中间虚线 xlim(-2.5,2.7) +ylim(-0.8,15) + #设置坐标轴范围 labs(x = expression(paste(log[2],"(fold change)")),y = expression(paste(-log[2],"(Q value)")), #加轴标题 title = "CMTM6 sgRNA2 vs Control",caption = "copy by R2-20")+ #加图题 theme(plot.title = element_text(hjust = mean(range(log2(myvolcano$FC)))+ 0.1)) +mytheme_volcano #主题设置
附件