前言
(个人学习笔记,供学习参考,如有不足之处,还请大佬们批评指正!!)
theme() 的语法有自己的一套风格,我感觉和ggplot2总体的风格类似,个人理解:它是针对图像的不同层次命名函数,并进行修改,并且后边的设置可以覆盖前面的设置,例子应该更能形象生动的展现。 在上一篇ggplot2-概述学习的结尾,也有讲到一部分theme的理解~: 【R语言-ggplot2学习-概览】
接下来跟着官方帮助文档学一些基本的。 先来画一个原始的散点图:
p1 <- ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
labs(title = "Fuel economy declines as weight increases")
p1
theme - 总体参数类型
以下是对图像整体的元素进行修改的参数,再细节一个层次的参数又主要分为3种:element_line;element_rect;element_text line all line elements (element_line())
rect all rectangular elements (element_rect())
text all text elements (element_text())
title all title elements: plot, axes, legends (element_text(); inherits from text)
aspect.ratio aspect ratio of the panel 例子:
p1 + theme(line = element_line(size = 1,linetype = 2,color = "black"))
改变网格线类型、大小,但不知道为什么,不能改变颜色。
p1 + theme(rect = element_rect(fill = "blue",color = "red",linetype = 2))
p1 + theme(text = element_text(colour = "red",size = 12,angle = 3))
p1 + theme(title = element_text(colour = "red",size = 10,angle = 2))
这个结果和上一条命令相似
p1 + theme(aspect.ratio = 1.5)
改变图像的长宽比,这个还是比较有用的!
theme - plot
以整幅图像为对象进行参数设置,不过只能调整,不能对没有的元素进行添加,添加元素使用其他命令。调整的主要也是字体大小颜色、位置、对齐等这些方面。 theme里头这类的参数有: plot.background, plot.title, plot.title.position, plot.subtitle, plot.caption, plot.caption.position, plot.tag, plot.tag.position, plot.margin, 尝试4个参设设置:
t.plot1<-p1 + labs(subtitle = "t.plot1")+
theme(plot.title = element_text(size = rel(2)))
t.plot2<-p1 + labs(subtitle = "t.plot2")+
theme(plot.background = element_rect(fill = "green"))
t.plot3<-p1 + labs(subtitle = "t.plot3")+
theme(plot.title.position = "plot")
t.plot4<-p1 + labs(subtitle = "t.plot4")+
theme(plot.margin = margin(t = 0,r = 0,b = 0,l = 1,unit = "cm"))
grid.arrange(t.plot1,t.plot2,t.plot3,t.plot4,ncol = 2)
theme - Panels
首先来一个实用技能!默认的灰色的背景个人觉得还是挺难看的,不知道为什么要选灰色作为默认的背景,不过都是可以修改哒!
p1 + theme(panel.background = element_rect(fill = "white", colour = "blue"))
p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))
同理,linetype = “solid”,就可以改成实线。
p1 + theme(panel.grid.major = element_line(colour = "black"))
p1 + theme(
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank()
)
p1 + theme(
panel.background = element_rect(fill = NA),
panel.grid.major = element_line(colour = "grey50"),
panel.ontop = T
)
网格线置于数据上方。
theme - axis
可以设置横纵坐标轴的刻度线、文本进行系列设置,包括大小、长度、颜色等。一张一张图地放有点累啊,还是就把代码和注释放着就好了。 举例比如说把刻度线长度设为0,等同于把刻度取消掉的效果,画图的时候有可能用上。
p1
p1 + theme(axis.line = element_line(size = 3, colour = "grey80"))
p1 + theme(axis.line = element_line(size = 1,colour = "black"))
p1 + theme(axis.text = element_text(colour = "blue"))
p1 + theme(axis.ticks = element_line(size = 2))
p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))
p1 + theme(
axis.ticks.length.y = unit(.25, "cm"),
axis.ticks.length.x = unit(-.25, "cm"),
axis.text.x = element_text(margin = margin(t = .3, unit = "cm"))
)
ttitle - legend
要展示图例的设置,需要另画一张图了。
p2 <- ggplot(mtcars, aes(wt, mpg)) +
geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
labs(
x = "Weight (1000 lbs)",
y = "Fuel economy (mpg)",
colour = "Cylinders",
shape = "Transmission"
)
p2
p2 + theme(legend.position = "none")
p2 + theme(legend.justification = "top")
p2 + theme(legend.position = "bottom")
p2 + theme(
legend.position = c(.95, .95),
legend.justification = c("right", "top"),
legend.box.just = "right",
legend.margin = margin(0, 6, 6, 6)
)
p2 + theme(
legend.box.background = element_rect(color= NA),
legend.box.margin = margin(6, 6, 6, 0)
)
p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))
p2 + theme(legend.text = element_text(size = 8, colour = "red"))
p2 + theme(legend.title = element_text(face = "bold"))
title - Strips
接下来是有关分面之后的设置
p3 <- ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
facet_wrap(~ cyl)
p3
p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))
p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))
p3 + theme(panel.spacing = unit(1, "lines"))
好啦,总体上的theme参数设置就这些啦,虽然还有些问题,之后的学习还是以问题为导向吧,需要对图形进行某种类型的设计的时候再来翻翻看搜搜看。(因为细节实在是好多啊呜呜呜)
如果学习笔记对屏幕前的你有那么一点帮助,点个赞鼓励一下哇! ^ _ ^
|