服务热线
178 0020 3020
作业一
library(ggplot2) plot1 <- ggplot(mtcars, aes(factor(cyl))) + geom_bar(aes(fill = factor(gear)), width = 0.6) + labs(title = "Here is title", subtitle = "Here is subtitle", caption = "R2-05", x = "cyl", fill = "gear")
# 更改图例名p1
print(plot1) mytheme <- theme_classic() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), #隐藏网格线 legend.position = "none", #移除图例,参考R数据可视化手册181-186页,关于移除图例的方法。 plot.caption = element_text(face = "bold.italic") ) plot1 + mytheme
library("ggplot2") library("latex2exp") #使用"latex2exp"包 进行输出数学符号
#自定义主题
mytheme <- theme_classic() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), #隐藏网格线 legend.position = "none", #移除图例,参考R数据可视化手册181-186页,关于移除图例的方法。 plot.caption = element_text(face = "bold.italic") )
##读入文件,为了后续画图进行编辑增加两列
myvolcano <- read.csv(file.choose()) head(myvolcano) myvolcano$significant <- ifelse(abs(log2(myvolcano$FC)) > 1 & myvolcano$q_value < 0.05,"yes","no" ) myvolcano$mylabels <- ifelse(abs(log2(myvolcano$FC)) > 1 & myvolcano$q_value < 0.05, as.character(myvolcano$Gene.ID),NA)
## subset data 因为要对途中显著差异的点标记label,没有想到好的方法 所以决定利用ggplot
## 的图形可以叠加显示的特点,利用新建一个数据框两次画图,叠加label,并设置颜色。
mysig <- subset(myvolcano,significant == "yes") mysig
##为了后续方便初始定义两个变量
ydrop = -0.6 textdrop = 0.3
#初始画图
myplot <- ggplot(myvolcano,aes(x = log2(FC) , y = -log2(q_value),size = significant)) + geom_point(col = "grey70",alpha = 1) + #设置所有的颜色为grey70 xlim(-2.5,2.7) + ylim(-0.8,15) + scale_size_manual(values = c(2,0)) + #自定义点的大小在这里显著差异的点的大小设置为0,为后续覆盖点做准备 geom_point(data = mysig,aes(x = log2(FC),y = -log2(q_value),col = mylabels), size = 4, #设置扰动,解决重叠问题,解决的也不好,求大神!!! position = position_jitter(height = 0.1,width = 0)) + geom_text(aes(label = mylabels),size =3,hjust = 0,vjust = -1,col = "black", fontface = "italic") + #设置label文本 #画箭头等细节 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) + #设置坐标轴标题的细节 labs(title = "CMTM6 sgRNA2 vs Control", caption = "create by R2-05") + #设置图注 labs(x = expression(log[2])) + #设置数学符号,请参考如何R 数学公式 labs(x = expression(paste(log[2],"(fold change)")), y = expression(paste(-log[2],"(Q value)")))
##设置主题 调用自己的主题
myplot + theme(plot.title = element_text(hjust = mean(range(log2(myvolcano$FC)))+ 0.1)) + geom_vline(xintercept = 0,linetype = 2 ,size= rel(0.8)) + # adding a line mytheme
附件