R语言ggplot2零散笔记~坐标轴放到右边/更改绘图边界/数据分组排序

ggplot2作图将Y轴的标签放到右边

正常坐标轴都是位于左边和下边,如果要改成上边或者右边可以使用如下代码

正常

library(ggplot2)
df<-data.frame(x=1:10,y=1:10)
ggplot(df,aes(x=x,y=y))+
  geom_point()
image.png

不正常

ggplot(df,aes(x=x,y=y))+
  geom_point()+
  scale_x_continuous(position = "top")+
  scale_y_continuous(position = "right")
image.png

ggplot2 title放中间

library(ggplot2)
ggplot()+
  geom_point(aes(x=1,y=1))+
  labs(title=expression(R^2==0.56))+
  theme(plot.title = element_text(hjust=0.5))
image.png

ggplot2更改绘图边界

library(ggplot2)
ggplot()+
  geom_point(aes(x=1,y=1))+
  labs(title=expression(R^2==0.56))+
  theme(plot.title = element_text(hjust=0.5),
        plot.margin = unit(c(1,4,1,1),"lines"))
image.png

四个位置 控制的分别是上右下 左

数据分组排序
> df<-data.frame(group=c("A","A","A","B","B","B"),
+                value=c(5,4,7,2,6,4))
> library(dplyr)

Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

> df%>%
+   group_by(group)%>%
+   arrange(group,value)
# A tibble: 6 x 2
# Groups:   group [2]
  group value
   
1 A         4
2 A         5
3 A         7
4 B         2
5 B         4
6 B         6

GO注释结果整理

GO注释的结果通常是两列,第一列是GO号,第二列是好多基因名,用逗号分隔。就是下面这种

GO0001 gene1,gene2
GO0002 gene5,gene3,gene4
GO0003 gene3,gene10

有时候我们需要把它整理成,单个基因对应一个GO号的形式

就是这种

 V1     V2    
     
1 GO0001 gene1 
2 GO0001 gene2 
3 GO0002 gene5 
4 GO0002 gene3 
5 GO0002 gene4 
6 GO0003 gene3 
7 GO0003 gene10

我最开始的解决办法是写简单的python脚本,昨天在一个微信群里看到有人给出的R语言代码,很好用,记录在这里

#install.packages("tidyr")
library(tidyr)
df<-read.table("../Some_data/pra.txt",header=F,sep=" ")
df%>%
  separate_rows(V2,sep=",")

欢迎大家关注我的公众号
小明的数据分析笔记本

你可能感兴趣的:(R语言ggplot2零散笔记~坐标轴放到右边/更改绘图边界/数据分组排序)