修改之后的图,接近发表的程度
本节的内容只有一个,就是对绘图的讲解。包括文本,轴线,颜色,图例,标题等等图形元素的调整,直到达到发表的水平。
代码解释
1.前面课程的代码重现
library(ggplot2)
filename <- "Lesson-05/Encode_HMM_data.txt"
my_data <- read.csv(filename, sep="\t", header=FALSE)
names(my_data)[1:4] <- c("chrom","start","stop","type")
# Reorder chromosomes and cut their names down
my_data$chrom <- factor(gsub("chr", "", my_data$chrom, fixed=TRUE), levels=c(seq(1,22),"X","Y"))
# Filter to just a few types I'm interested in
my_data <- my_data[my_data$type %in% c("1_Active_Promoter","4_Strong_Enhancer","8_Insulator"),]
# Rename the types
library(plyr) # this library has a useful revalue() function
my_data$type <- revalue(my_data$type, c("1_Active_Promoter"="Promoter", "4_Strong_Enhancer"="Enhancer","8_Insulator"="Insulator"))
# Check the plot again
ggplot(my_data,aes(x=chrom,fill=type)) + geom_bar()
# 到此,以上的代码部分都是前几次课注释过了。
2.字体和标签的修改
# The basics
ggplot(my_data,aes(x=chrom,fill=type)) + geom_bar()
# 添加图题,使用函数labs(title = "")
# Add a plot title
ggplot(my_data,aes(x=chrom,fill=type)) + geom_bar() + labs(title="Regulatory features by chromosome")
# 同样改变x轴和图例的标签
# Change axis and legend labels
ggplot(my_data,aes(x=chrom,fill=type)) + geom_bar() + labs(x = "Chromosome",y="Count",fill="Feature")
#可以将修改labels后的画图结果保存到basic变量中,便于之后的在基础上尝试新的元素
# Save the plot to easily try new things:
basic <- ggplot(my_data,aes(x=chrom,fill=type)) + geom_bar() + labs(x = "Chromosome",y="Count",fill="Feature")
# Now when we run "basic" it makes the plot
basic
用theme来修改基本的绘图,增大字体,base_size相当于font size
# Add theme with modifications to the basic plot, for instance with bigger text
basic + theme_gray(base_size = 20)
# But it only affects that plot, so the next plot we make is back to normal
basic
# 可以通过theme_set()来设定,从而影响接下去画的图
# You can also set a theme that will affect all the plots you make from now on
theme_set(theme_gray(base_size = 20))
basic
# 回复原先的默认值
# To recover the default theme:
theme_set(theme_gray())
basic
# 将字体改成16
# I prefer larger text myself
theme_set(theme_gray(base_size = 16))
basic
3.颜色填充设置
# Color palettes:
# RColorBrewer是一个R包,包含了很多的颜色板
library(RColorBrewer)
# 来查看display中所有的颜色
display.brewer.all()
# ggplot2()绘图就是可以通过加号“+”不断地来添加你所需要的,来修改你的图片。通过scale_fill_brewer(palette="")函数,来自动替换基本图片中的填充颜色。
basic + scale_fill_brewer(palette="Set1")
basic + scale_fill_brewer(palette="Pastel1")
basic + scale_fill_brewer(palette="YlOrRd")
# 也可以手动换填充色scale_fill_manual(values = )函数
basic + scale_fill_manual(values = c("green","blue","red"))
# R自带的颜色就有很多,color()可以显示所有的颜色,大概显示657种色彩,应该已经足够丰富了
colors()
# 如果填充颜色不够,那么我们该怎么板,当然也是有办法解决的
# What happens if we need a lot of colors?
chrom_plot <- ggplot(my_data,aes(x=type,fill=chrom)) + geom_bar()
chrom_plot
# 这里就显示颜色不够,而且也不清楚,那么该怎么解决?
# rainbow is confusing, but color palettes are too short:
chrom_plot + scale_fill_brewer(type="qual",palette=1)
# 可以将多个颜色板组合,得到更多的颜色,这里将set1,set2,set3的共26种颜色组合成一个大的颜色板
# to get the colors from a palette:
palette1 <- brewer.pal(9,"Set1")
palette1
palette2 <- brewer.pal(8,"Set2")
palette2
palette3 <- brewer.pal(9,"Set3")
palette3
# 用饼图可以查看颜色的种类
# We can use a quick pie chart to see the colors:
pie(rep(1,length(palette1)),col=palette1)
pie(rep(1,length(palette2)),col=palette2)
pie(rep(1,length(palette3)),col=palette3)
# 颜色的组合
# We can just stick a few color palettes together
big_palette <- c(palette1,palette2,palette3)
big_palette
# Pie chart of all the colors together:
pie(rep(1,length(big_palette)),col=big_palette)
# 用重新组合的大颜色板重新绘图
chrom_plot + scale_fill_manual(values = big_palette)
# sample()可以打乱颜色的顺序
# To shuffle the colors:
chrom_plot + scale_fill_manual(values = sample(big_palette))
# 当然如果你需要每次画图都是同样的颜色,那么可以设置随机种子,set.seed(),当然这也不是真正的随机,所以只要数字一样,每次的随机也是一样的,因此这个种子的颜色是不会变的。
# if you want to keep the colors the same every time you plot,
# use set.seed()
set.seed(5)
# use different numbers until you find your favorite colors
chrom_plot + scale_fill_manual(values = sample(big_palette))
# 因为一部分人是存在颜色辨认困难,也就是色盲,因此最好还是使用色盲安全色来进行绘图,这样能够减去不必要的麻烦。
# Color-blind safe palettes:
display.brewer.all(colorblindFriendly=TRUE)
# Mixing them might remove the color-friendly-ness so be careful
# Finding a set of 23 colors that a color-blind person can distinguish is a challenge
basic + scale_fill_brewer(palette="Set2")
# 到此,填充色变换已经讲解完成。
颜色很模糊,没有办法区分
大颜色板
换颜色后重新绘制,区分更加明显
shuffle color
4. 绘制具有发表水平的图片
# 首先恢复默认设置
# Default:
theme_set(theme_gray())
# 画出基本的图
# Basic, normal plot:
basic
有两种基础的主题
# Two basic themes:
basic + theme_gray() # the default
basic + theme_bw() # black and white
# base_size和base_family分别代表字体大小,字体类型
# Fonts and font sizes for everything at once
basic + theme_gray(base_size = 24, base_family = "Times New Roman")
# 分别设置标签,分割线标签,图例标签的字体大小,可以用theme()
# Font size for labels, tick labels, and legend separately
basic + theme(axis.text=element_text(size=20)) # 轴上数字的大小
basic + theme(axis.title=element_text(size=20)) #轴上标题的大小
basic + theme(legend.title=element_text(size=20)) # 图例标题的大小
basic + theme(legend.text=element_text(size=20,family="Times New Roman")) # 图例类别字体的大小
# 一个语句同时设置(两种方法都可以)
basic + theme(
legend.text=element_text(size=20,family="Times New Roman"),
axis.title=element_text(size=30),
axis.text=element_text(size=20)
) # Mix and match
# 改变背景颜色,
# Change background color
basic + theme(panel.background = element_rect(fill="pink"))
basic + theme(panel.background = element_rect(fill="white"))
# 添加主要网格线和次要网格线,并设置颜色
# Change grid-lines
basic + theme(panel.grid.major = element_line(colour = "blue"), panel.grid.minor = element_line(colour = "red"))
# 移除所有的网格线
basic + theme(panel.grid.major = element_line(NA), panel.grid.minor = element_line(NA))
# 移除垂直于x轴的所有网格线,画黑色的主要的线。
# Thin black major gridlines on y-axis, the others are removed
basic + theme(panel.grid.major.y = element_line(colour = "black",size=0.2), panel.grid.major.x = element_line(NA),panel.grid.minor = element_line(NA))
# 改变刻度线的颜色,大小
# Change tick-marks
basic # normal ticks
basic + theme(axis.ticks = element_line(size=2))
basic + theme(axis.ticks = element_line(NA))
basic + theme(axis.ticks = element_line(color="blue",size=2))
basic + theme(axis.ticks = element_line(size=2), # affects both x and y
axis.ticks.x = element_line(color="blue"), # x only
axis.ticks.y = element_line(color="red")) # y only
# 改变图例的位置
# Place legend in different locations
basic + theme(legend.position="top")
basic + theme(legend.position="bottom")
basic + theme(legend.position=c(0,0)) # bottom left
basic + theme(legend.position=c(1,1)) # top right
basic + theme(legend.position=c(0.8,0.8)) # near the top right
# 移除图例的标题
# Remove legend title
basic + labs(fill="")
basic + labs(fill="") + theme(legend.position=c(0.8,0.8))
彻底移除图例
# Remove legend completely
basic + guides(fill=FALSE)
# 绘制可以发表的风格图
# clear background, axis lines but no box, no grid lines, basic colors, no legend
publication_style <- basic + guides(fill=FALSE) + theme(axis.line = element_line(size=0.5),panel.background = element_rect(fill=NA,size=rel(20)), panel.grid.minor = element_line(colour = NA), axis.text = element_text(size=16), axis.title = element_text(size=18))
publication_style
# 使图片不会悬浮与x轴之上
publication_style + scale_y_continuous(expand=c(0,0))
# theme_set永不所有之后的图片绘制
theme_set(theme_gray()+theme(axis.line = element_line(size=0.5),panel.background = element_rect(fill=NA,size=rel(20)), panel.grid.minor = element_line(colour = NA), axis.text = element_text(size=16), axis.title = element_text(size=18)))
basic
# 不在里面的设置,依旧需要分开各自添加参数
# These tweaks aren't part of the theme, so you will still have to add them separately to each plot
basic + scale_y_continuous(expand=c(0,0)) + guides(fill=FALSE)
# 任何时候都可以重新回到最初的图
theme_set(theme_gray())
basic
默认的theme_gray()
theme_bw()
改变刻度线的颜色和粗细