R绘图的生物学家(5):Tweaking everything in your plots

修改之后的图,接近发表的程度

本节的内容只有一个,就是对绘图的讲解。包括文本,轴线,颜色,图例,标题等等图形元素的调整,直到达到发表的水平。

代码解释

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()
改变刻度线的颜色和粗细
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,734评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,931评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,133评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,532评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,585评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,462评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,262评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,153评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,587评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,792评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,919评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,635评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,237评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,855评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,983评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,048评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,864评论 2 354

推荐阅读更多精彩内容